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

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

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