String Conversion To Atom

From Erlang Community

(Difference between revisions)
Revision as of 21:16, 18 August 2006 (edit)
Cyberlync (Talk | contribs)

← Previous diff
Current revision (19:09, 24 September 2006) (edit) (undo)
Ayrnieu (Talk | contribs)
m (formatting)
 
(One intermediate revision not shown.)
Line 8: Line 8:
<code> <code>
-For example: 1> list_to_atom("hello").+1> list_to_atom("hello").
hello hello
2> atom_to_list(hello). 2> atom_to_list(hello).
Line 23: Line 23:
-[[Category:CookBook]]+[[Category:CookBook]][[Category:StringRecipes]]

Current revision

[edit] Problem

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

[edit] 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"

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
Erlang/OTP Projects
Personal tools