Skip to content

Strings ​

Quotes ​

"…" understands escapes and interpolation. '…' is a raw string: only \' and \\ are escaped.

mzs
"a\tb".len  # → 3
'a\tb'.len  # → 4
'it\'s'  # → "it's"

Both forms may span several lines — the line break in the source goes into the value.

Interpolation ​

FormWhat it inserts
${expression}any expression, a local variable included
$namethe host variable $name
$ with no name and no {the $ sign itself
mzs
name = "Ann"
items = [3, 4]
"Hello, ${name}!"  # → "Hello, Ann!"
"sum: ${items.sum * 10} ₽"  # → "sum: 70 ₽"
"price: 100 $"  # → "price: 100 $"
"${nil}|${true}|${[1, 2]}|${{a: 1}}"  # → "|true|[1,2]|{\"a\":1}"

A local variable goes only in ${…}. "$name" reads the host variable, not the local one:

mzs
name = "Ann"
"$name"  # → ""
"${name}"  # → "Ann"

'…' inserts nothing: '${x}' stays as is.

Escapes ​

EscapeValue
\n \t \r \0line feed, tab, carriage return, null character
\\ \" \'backslash, quotes
\$the $ sign
\u{X…}a character by code point, 1–6 hex digits: \u{1F600}
\x41a byte
mzs
"\u{1F600}"  # → "😀"
"\x41"  # → "A"
"10\$ off"  # → "10$ off"

Heredoc ​

<<~TAG takes the lines below up to the line TAG and strips the common indent. <<~'TAG' — no interpolation, no escapes.

mzs
name = "Ann"
text = <<~MSG
  Hello, ${name}!
    Your order is ready.
MSG
text  # → "Hello, Ann!\n  Your order is ready.\n"
mzs
raw = <<~'TPL'
  ${not substituted} and \n are two characters
TPL
raw  # → "${not substituted} and \\n are two characters\n"

The body always ends with a line break.

Characters, not bytes ​

Length, indices and slices count Unicode characters.

mzs
s = "привет"
s.len  # → 6
s.bytes.len  # → 12
s[0]  # → "п"
s[-1]  # → "т"
s[1, 3]  # → "рив"
s[10]  # → nil
"abc".chars  # → ["a","b","c"]
"я".upper  # → "Я"

s[i, n] is the substring of length n from position i. An index out of bounds gives nil, not an error.

Strings are immutable, every method returns a new string:

mzs
s = "cat"
s[0] = "C"
text
type: cannot assign to an index of string

Common methods ​

MethodExample
upper lower"Ok".lower → "ok"
trim" x ".trim → "x"
split"a,b".split(",") → ["a","b"]
join["a", "b"].join("-") → "a-b"
replace"a-b".replace("-", "+") → "a+b"
has"hello".has("ell") → true
int float"42".int → 42
mzs
"  Hello, World ".trim.lower  # → "hello, world"
"a-b-c".split("-")  # → ["a","b","c"]
"12abc".int  # → 12
"abc".int  # → 0

.int and .float never fail: what does not parse becomes 0. The full list is in Strings in the library.

Formatting: % ​

template % arguments is the same as format(template, …). A single argument need not be wrapped in an array.

SpecifierWhat it prints
%sthe value as a string
%dan integer
%f %.2fa float
%x %o %ban integer in base 16, 8, 2
%jJSON
%%the % sign
mzs
"%s: %d pcs" % ["apples", 3]  # → "apples: 3 pcs"
"%.2f" % 3.14159  # → "3.14"
"%05.1f" % 3.14159  # → "003.1"
"%-6s|" % "ab"  # → "ab    |"
"%x" % 255  # → "ff"
"%d%%" % 50  # → "50%"
"%<name>s, %<age>d" % {name: "Ann", age: 30}  # → "Ann, 30"
format("%s-%s", "a", "b")  # → "a-b"