Double-Quoted Strings Are Binaries

Whereas single-quoted strings are stored as char lists, the contents of a double-quoted string (dqs) are stored as a consecutive sequence of bytes in UTF-8 encoding. Clearly this is more efficient in terms of memory and certain forms of access, but it does have two implications.

First, because UTF-8 characters can take more than a single byte to represent, the size of the binary is not necessarily the length of the string.

 iex>​ dqs = ​"​​∂x/∂y"
 "∂x/∂y"
 iex>​ String.length dqs
 5
 iex>​ byte_size dqs
 9
 iex>​ String.at(dqs, 0)
 "∂"
 iex>​ String.codepoints(dqs)
 ["∂", "x", "/", "∂", "y"]
 iex>​ String.split(dqs, ​"​​/"​)
 ["∂x", "∂y"]

Second, because you’re no longer using lists, you need to learn ...

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.