String Literals
Explore how Elixir handles string literals such as single-quoted and double-quoted strings, including UTF-8 characters and escape sequences. Understand multiline strings with heredocs and discover the power of sigils for various literal types. This lesson helps you master string manipulation and interpolation techniques essential for effective Elixir programming.
Strings
Elixir has two kinds of strings: single-quoted and double-quoted. They differ significantly in their internal representation. But they also have many things in common. For example, see the following:
- Strings can hold characters in UTF-8 encoding.
- They may contain escape sequences:
`\a` BEL (0x07) `\e` ESC (0x1b) `\v` VT (0x0b) `\b` BS (0x08) `\f` FF (0x0c) `\s` SP (0x20) `\d` DEL (0x7f) `\n` NL (0x0a) `\t` TAB (0x09) `\r` CR (0x0d) `\xhh` 2 hex digits `\uhhh` 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.