Single-Quoted Strings—Lists of Character Codes
Single-quoted strings are represented as a list of integer values, each value corresponding to a codepoint in the string. For this reason, we refer to them as character lists (or char lists).
| iex> str = 'wombat' |
| 'wombat' |
| iex> is_list str |
| true |
| iex> length str |
| 6 |
| iex> Enum.reverse str |
| 'tabmow' |
This is confusing: IEx says it is a list, but it shows the value as a string. That’s because IEx prints a list of integers as a string if it believes each number in the list is a printable character. You can try this for yourself:
| iex> [ 67, 65, 84 ] |
| 'CAT' |
You can look at the internal representation in a number of ways:
| iex> str = 'wombat' |
| 'wombat' |
| iex> :io.format "~w~n" |
Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.