Erlang Central

String Conversion To Atom

From ErlangCentral Wiki

Problem

You want to convert an atom to a string or vice versa

Solution

The built-in function list_to_atom converts from a string to an atom, and atom_to_list does the conversion in the other direction.


1> list_to_atom("hello").
hello
2> atom_to_list(hello).
"hello"

Erlang only has a limited space for atoms, using list_to_atom on arbitrary lists can cause the emulator to terminate when this limit is reached. It is possible to use list_to_existing_atom/1 with the caveat that it would return a badarg if the atom currently doesn't exist.

Note that by default Erlang is case sensitive, so the atom hello, Hello and HeLLo are unique:


1> hello = hEllo.
** exited: {{badmatch,hEllo},[{erl_eval,expr,3}]} **
2> hello = hello.
hello