TExcelTAD
Home › Elixir › Elixir Binaries, Strings & Charlists

Elixir Binaries, Strings & Charlists

An Elixir string is a binary: a sequence of bytes, encoded in UTF-8. Understanding the three related ideas, code points, bytes and graphemes, saves you from many surprises when text is not plain English.

Strings are UTF-8 binaries

elixir · try it yourself
word = "naïve"

IO.inspect(String.length(word))     # graphemes (what a reader calls characters)
IO.inspect(byte_size(word))         # bytes
IO.inspect(String.codepoints(word))
IO.inspect(word, binaries: :as_binaries)
IO.inspect(is_binary(word))
5
6
["n", "a", "ï", "v", "e"]
<<110, 97, 195, 175, 118, 101>>
true

ï is one character but two bytes (195, 175). binaries: :as_binaries shows the raw bytes that make up the string.

Term Meaning Function
byte 8 bits of storage byte_size/1
code point a Unicode number such as ?a = 97 String.codepoints/1
grapheme what a user sees as one character (may be several code points) String.length/1, String.graphemes/1

?a gives the code point of a character: ?a is 97, ?é is 233.

Useful String functions

elixir · try it yourself
title = "  The Pragmatic Programmer  "

IO.inspect(String.trim(title))
IO.inspect(String.downcase(title) |> String.trim())
IO.inspect(String.split("red,green,blue", ","))
IO.inspect(String.replace("2026-01-15", "-", "/"))
IO.inspect(String.contains?("elixir lang", "lang"))
IO.inspect(String.starts_with?("report.csv", "report"))
IO.inspect(String.slice("Elixir", 1..3))
IO.inspect(String.pad_leading("42", 5, "0"))
IO.inspect(String.reverse("stressed"))
"The Pragmatic Programmer"
"the pragmatic programmer"
["red", "green", "blue"]
"2026/01/15"
true
true
"lix"
"00042"
"desserts"

Functions ending in ? return a boolean; this is a naming convention, not syntax.

Interpolation, escapes and multi-line strings

elixir · try it yourself
item = "coffee"
qty = 3
IO.puts("#{qty} x #{item} = #{qty * 2.5} EUR")
IO.puts("Tab:\there, quote: \", newline below\nsecond line")

receipt = """
Order summary
  item: #{item}
  qty:  #{qty}
"""

IO.write(receipt)
3 x coffee = 7.5 EUR
Tab:	here, quote: ", newline below
second line
Order summary
  item: coffee
  qty:  3

A heredoc (""" … """) keeps the line breaks. Its indentation is removed based on the position of the closing """. IO.write/1 prints without adding a newline.

Binary pattern matching

Because strings are binaries, you can match on them. "prefix" <> rest matches a string that starts with a known prefix:

elixir · try it yourself
parse = fn
  "GET " <> path -> {:get, path}
  "POST " <> path -> {:post, path}
  _ -> :unknown
end

IO.inspect(parse.("GET /users"))
IO.inspect(parse.("POST /orders"))
IO.inspect(parse.("DELETE /x"))

<<first_byte, rest::binary>> = "hello"
IO.inspect(first_byte)
IO.inspect(rest)
{:get, "/users"}
{:post, "/orders"}
:unknown
104
"ello"

The <<...>> syntax builds and matches binaries byte by byte. <<first_byte, rest::binary>> takes the first byte as an integer and the remaining bytes as a binary. Binary syntax is also how Elixir parses network packets and file formats.

Charlists

Single quotes create a charlist: a list of code points. Charlists exist mainly for calling Erlang libraries, which often expect them. Since Elixir 1.15 they are written and printed with the ~c sigil:

elixir · try it yourself
chars = ~c"hello"
IO.inspect(chars)
IO.inspect(is_list(chars))
IO.inspect(chars == [104, 101, 108, 108, 111])

IO.inspect(to_string(chars))
IO.inspect(to_charlist("abc"))
IO.inspect([72, 105])
~c"hello"
true
true
"hello"
~c"abc"
~c"Hi"

Note the last line: a list of integers that are all printable characters is shown as a charlist. [72, 105] is ~c"Hi", it is the same value. If you want to see the numbers, use IO.inspect(list, charlists: :as_lists).

Common mistakes

  • 'text' vs "text": single quotes give a charlist, which String functions do not accept.
  • Using length/1 on a string: length is for lists. Use String.length/1.
  • Using String.length/1 in a hot loop to check emptiness: it walks the whole string. Compare with "" or use byte_size(s) == 0.
  • Joining with +: strings are joined with <> (or interpolation).

Test yourself

What does ~c"ab" == [97, 98] return?

  1. true
  2. false
  3. An error
  4. "ab"

Which pattern matches strings that start with "id:"?

  1. "id:" + rest
  2. "id:" <> rest
  3. ["id:" | rest]
  4. rest <> "id:"

Why can byte_size(s) be bigger than String.length(s)?

  1. Strings are padded
  2. Non-ASCII characters use several bytes in UTF-8
  3. byte_size includes a terminator
  4. String.length ignores spaces