Strings in Elixir are "binaries". However, in Erlang code, strings are traditionally "char lists", so when calling Erlang functions, you may have to use char lists instead of regular Elixir strings.
While regular strings are written using double quotes "
, char lists are written using single quotes '
:
string = "Hello!"
char_list = 'Hello!'
Char lists are simply lists of integers representing the code points of each character.
'hello' = [104, 101, 108, 108, 111]
A string can be converted to a char list with to_charlist/1
:
iex> to_charlist("hello")
'hello'
And the reverse can be done with to_string/1
:
iex> to_string('hello')
"hello"
Calling an Erlang function and converting the output to a regular Elixir string:
iex> :os.getenv |> hd |> to_string
"PATH=/usr/local/bin:/usr/bin:/bin"