Strings and binaries

Strings are lists

Text in double quotes is a string, and an Erlang string is a list of character codes. The shell prints a printable list back in quotes, so the same list can be typed either way.

The list [104, 101, 108, 108, 111] is the word hello: each number is one character.

erl

1> "hello".
"hello"
2> [104, 101, 108, 108, 111].
"hello"

Binaries

A binary is a sequence of bytes written between double angle brackets. It is the compact form for input and output, and UTF-8 text fits directly.

Single quotes do not build a string: they build an atom, so hello in single quotes is the atom hello, not the text.

erl

1> <<"hello">>.
<<"hello">>
2> byte_size(<<"hello">>).
5

Join and split

The string module joins, splits, trims, and searches. Its functions understand Unicode.

Splitting answers with a list of strings, one per piece, ready for the list tools of lesson 12.

erl

1> string:join(["a", "b"], ",").
"a,b"
2> string:split("a,b", ",").
["a","b"]

Size and length

byte_size counts bytes in a binary, and size answers the same for a binary. length counts the items of a list, so it counts the characters of a string.

For a UTF-8 binary the byte count can exceed the character count, so pick the measure your logic needs.

erl

1> length("hello").
5
2> size(<<"hello">>).
5

Next: Truthiness: only false and nil

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is an Erlang string such as "hello"?

    Answer

    A list of character codes

    Double quotes build a list whose items are the character codes of the text.

  2. What does [104, 101, 108, 108, 111]. print?

    Answer

    "hello"

    The shell prints a list of printable character codes as a string in quotes.

  3. How do you write a binary?

    Answer

    <<"hello">>

    Double angle brackets mark a binary, and the text inside is stored as bytes.

  4. What does 'hello'. build?

    Answer

    The atom hello

    Single quotes build an atom, never a string, so the text is a name rather than data.

  5. What does byte_size(<<"hello">>). return?

    Answer

    5

    The binary holds five bytes, so its size is the number 5.

  6. What does length("hello"). return?

    Answer

    The number of characters in the list

    A string is a list, and length counts its items, one per character.

  7. What does string:join(["a", "b"], ","). return?

    Answer

    "a,b"

    The function puts the separator between the pieces, giving one string "a,b".

  8. What does string:split("a,b", ","). return?

    Answer

    ["a","b"]

    Splitting at the comma gives a list with one string per piece.

  9. For a UTF-8 binary, why can byte_size be larger than string:length?

    Answer

    One character may take several bytes

    string:length counts characters, while byte_size counts bytes; a non-ASCII character takes more than one byte.

  10. Which function counts the bytes of a binary?

    Answer

    byte_size/1

    byte_size/1 answers the byte count; size/1 and length/1 read other data shapes.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev