O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required