String Literals

Elixir has two kinds of string: single-quoted and double-quoted. They differ significantly in their internal representation. But they also have many things in common.

  • Strings can hold characters in UTF-8 encoding.

  • They may contain escape sequences:

    \a

    BEL (0x07)

    \b

    BS (0x08)

    \d

    DEL (0x7f)

    \e

    ESC (0x1b)

    \f

    FF (0x0c)

    \n

    NL (0x0a)

    \r

    CR (0x0d)

    \s

    SP (0x20)

    \t

    TAB (0x09)

    \v

    VT (0x0b)

    \xhhh

    1–6 hex digits

  • They allow interpolation on Elixir expressions using the syntax #{...}.

     
    iex>​ name = "dave"
     
    "dave"
     
    iex>​ "Hello, #{String.capitalize name}!"
     
    "Hello, Dave!"
  • Characters that would otherwise have special meaning can be escaped with a backslash.

  • They support heredocs.

Heredocs

Any string can span several lines. To illustrate this, we’ll use both ...

Get Programming Elixir 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.