Strings
Single and double quotes are identical. Both support interpolation, escape sequences, and all string methods.
let s1 = "hello world"
let s2 = 'also a string'
-- concatenation
let joined = "hello" ++ " world"
println(joined)
-- length (codepoints, not bytes)
println("hello".len()) -- 5
Any expression inside { braces gets evaluated and embedded in the string. Escape a brace with \{ to suppress interpolation.
let name = "XS"
println("Hello, {name}!") -- Hello, XS!
println("{1 + 2} is three") -- 3 is three
println("len: {name.len()}") -- len: 2
println("\{literal brace}") -- {literal brace}
Add :spec after an interpolated expression to control formatting. The spec follows Python's mini-language: [fill][align][width][,][.prec][type].
let pi = 3.14159
println("{pi:.2}") -- 3.14
println("{pi:8.2}") -- 3.14
println("{pi:<8.2}") -- 3.14
println("{pi:^8.2}") -- 3.14
println("{pi:.2%}") -- 314.16%
let n = 1234567
println("{n:,}") -- 1,234,567
println("{255:x}") -- ff
println("{8:b}") -- 1000
println("{42:0>5}") -- 00042
Prefix r to disable escape processing and interpolation. Useful for regex patterns and file paths.
let pattern = r"\d+\.\d+"
println(pattern) -- \d+\.\d+
let x = 42
let raw = r"no {x} here \n raw"
println(raw) -- no {x} here \n raw
Use """ """ for multi-line strings. Indentation is preserved and interpolation still works. Prefix with r for raw triple-quoted.
let text = """
line one
line two
line three
"""
println(text.contains("line two")) -- true
Prefix c to embed ANSI terminal colors at parse time. The format is c"style;style;...;text" where the last segment is the text.
let err = c"bold;red;Error: something went wrong"
let ok = c"green;Done"
println(err)
println(ok)
let s = "Hello, World!"
println(s.upper()) -- HELLO, WORLD!
println(s.lower()) -- hello, world!
println(s.contains("World")) -- true
println(s.starts_with("Hello")) -- true
println(s.replace("World", "XS")) -- Hello, XS!
println(s.split(", ")) -- [Hello, World!]
println(" hi ".trim()) -- hi
println("ha".repeat(3)) -- hahaha
println(s.slice(0, 5)) -- Hello