Strings
| Double-quoted strings | |
| Interpolation #{} | |
| Heredocs | |
| Handy String functions |
Double-quoted strings
Elixir strings live in double quotes and hold UTF-8 text. A string is a binary under the hood, but you can treat it as plain text for now:
greeting = "hello"
IO.puts(greeting) # prints hello
Interpolation #{}
Interpolation drops any value into a string with the hash brace pair. The expression inside runs first, turns into text, and joins the surrounding words:
name = "Ada"
IO.puts("Hello, #{name}") # prints Hello, Ada
IO.puts("Next year: #{36 + 1}") # prints Next year: 37
Heredocs
Triple quoted heredocs span several lines without plus signs or escapes. The closing quotes set the indentation that gets stripped from every line:
text = """
hello
world
"""
IO.puts(text) # prints two lines
hello
world
Handy String functions
The String module packs the everyday tools: trimming, casing, and measuring. Pipes keep chained calls flat and readable:
" hello " |> String.trim() |> String.upcase() # gives "HELLO"
String.length("hello") # gives 5
Next: Atoms
Article author: Arthur Isaev