Elixir Cheatsheet

Strings, Binaries, and Sigils

Use this Elixir reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

String Essentials

String.length("héllo")            # 5 (graphemes, not bytes)
byte_size("héllo")                # 6 (UTF-8 bytes)
String.upcase("abc")              # "ABC"
String.downcase("ABC")
String.capitalize("elixir")       # "Elixir"
String.trim("  hi  ")             # "hi"
String.split("a,b,c", ",")        # ["a", "b", "c"]
String.split("a b  c")            # on whitespace: ["a", "b", "c"]
String.replace("a-b-c", "-", "_") # "a_b_c"
String.contains?("team", "ea")    # true
String.starts_with?("file.exs", "file")
String.ends_with?("file.exs", [".ex", ".exs"])
String.slice("elixir", 0, 3)      # "eli"
String.pad_leading("7", 3, "0")   # "007"
String.duplicate("ab", 3)         # "ababab"
String.graphemes("héllo")         # ["h", "é", "l", "l", "o"]
String.to_integer("42")           # 42; Integer.parse/1 for safe parsing
String.reverse("abc")             # "cba"

Strings Are Binaries

An Elixir string is a UTF-8 encoded binary. is_binary("abc") is true.

"foo" <> "bar"                    # "foobar"
<<104, 105>>                      # "hi" — binaries are sequences of bytes
?a                                # 97 — code point of a character

Binary Pattern Matching

"hello " <> rest = "hello world"      # rest = "world" (prefix match only)
<<first::binary-size(2), rest::binary>> = "abcdef"  # "ab" / "cdef"
<<code::utf8, rest::binary>> = "héllo"              # code = 104, decodes one code point
<<len::16, body::binary-size(len)>> = packet        # parse length-prefixed frames
<<r, g, b>> = <<255, 128, 0>>                       # unpack bytes

Segment modifiers: integer (default), binary/bytes, utf8, float, size(n), unit(n), little/big, signed/unsigned — combined with -, e.g. <<x::little-signed-32>>.

Charlists

~c"hello"                 # charlist: [104, 101, 108, 108, 111]
to_string(~c"hello")      # "hello"
to_charlist("hello")      # ~c"hello"

Charlists exist for Erlang interop (:file, :httpc, etc.). Use double-quoted strings everywhere else. The legacy 'hello' literal is soft-deprecated since 1.15; the formatter rewrites it to ~c"hello".

Sigils

~s(a "quoted" string)         # string without escaping quotes
~S(no #{interpolation})       # uppercase = no interpolation or escapes
~w(alpha beta gamma)          # word list: ["alpha", "beta", "gamma"]
~w(ok error)a                 # atom list: [:ok, :error]  (modifiers: a s c)
~c(hello)                     # charlist
~r/^\d+$/                     # regex; modifiers like ~r/abc/i
~D[2026-01-15]                # Date
~T[13:30:00]                  # Time
~U[2026-01-15 13:30:00Z]      # DateTime (UTC)
~N[2026-01-15 13:30:00]       # NaiveDateTime

Delimiters are flexible: ~s(...), ~s{...}, ~s[...], ~s/.../, ~s"...". Define custom sigils with defmacro sigil_x/2.

Regex

Regex.match?(~r/^\d+$/, "123")            # true
Regex.run(~r/(\w+)@(\w+)/, "a@b")         # ["a@b", "a", "b"]
Regex.scan(~r/\d+/, "a1 b22")             # [["1"], ["22"]]
Regex.replace(~r/\s+/, "a  b", " ")       # "a b"
String.match?("abc123", ~r/\d/)           # true
String.split("a1b22c", ~r/\d+/)           # ["a", "b", "c"]
"user@host" =~ ~r/@/                      # true (=~ works with regex or string)

Named captures: Regex.named_captures(~r/(?<user>\w+)@/, "ada@x") returns %{"user" => "ada"}.

Heredocs and Multiline

text = """
Multiline string.
Interpolation #{works} here.
"""

Heredocs (""") strip the indentation of the closing delimiter — this is also the @doc/@moduledoc convention.

Converting and Building Text

to_string(42)                       # "42"
Integer.to_string(255, 16)          # "FF"
inspect(%{a: 1})                    # "%{a: 1}" — any term, for logs/debugging
Enum.join(["a", "b"], ", ")         # "a, b"
Enum.map_join(1..3, "-", &to_string/1)  # "1-2-3"
IO.iodata_to_binary(["a", ["b", "c"]])  # "abc" — iolists avoid concat cost

Prefer iodata ([part1, part2]) over repeated <> concatenation when building large output; IO.puts and Phoenix accept iodata directly.