Strings
Literals
Section titled “Literals”Strings are written in double or single quotes. They are different:
- Double quotes process backslash escapes such as
\nand\t, and interpolate${...}. - Single quotes are raw: backslashes and
${...}are kept as written, which makes them the natural choice for regular expressions, Windows paths and templates.
name = "Kiwi"println "Hello, ${name}!\tTabbed" # Hello, Kiwi! Tabbedprintln 'Hello, ${name}!\tRaw' # Hello, ${name}!\tRawprintln 'C:\Users\kiwi' # C:\Users\kiwiA raw string can’t contain a single quote. Use a double-quoted string for that.
Both kinds can span multiple lines; the line breaks become part of the string.
poem = "Roses are red,Kiwis are green."println poemTo strip the indentation of a multi-line string, use .dedent() from the string package.
Interpolation
Section titled “Interpolation”Inside a double-quoted string, ${expression} is replaced by the value of the expression. Any expression works, including method calls, arithmetic and nested strings.
x = 50y = 10println "${x} / ${y} = ${x / y}" # 50 / 10 = 5println "upper: ${"kiwi".uppercase()}" # upper: KIWIprintln "items: ${[1, 2, 3].size()}" # items: 3println "price: $5" # price: $5A $ that isn’t followed by { is an ordinary character. To print a literal ${, escape the dollar sign: "\${".
Escape sequences
Section titled “Escape sequences”These escapes work in double-quoted strings:
| Escape | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\r | Carriage return |
\b | Backspace |
\f | Form feed |
\\ | Backslash |
\" | Double quote |
\$ | Dollar sign (prevents interpolation) |
\xHH | Character with code 0x00 to 0xFF, 2 hex digits |
\uXXXX | Unicode code point, 4 hex digits |
\UXXXXXXXX | Unicode code point, 8 hex digits |
println "Quote: \"hi\"" # Quote: "hi"println "\x4B\x69\x77\x69" # Kiwiprintln "café" # caféprintln "\U0001F95D" # 🥝Indexing and slicing
Section titled “Indexing and slicing”Strings are indexed by character from 0. A slice s[start:end] takes characters from start up to, but not including, end; either bound can be left out. A third value sets the step, and a step of -1 reverses.
s = "kiwifruit"println s[0] # kprintln s[0:4] # kiwiprintln s[4:] # fruitprintln s[::2] # kwfutprintln s[::-1] # tiurfiwikNegative indexes are not supported. Use s[s.size() - 1] for the last character.
Strings are immutable: methods like .uppercase() return a new string.
Common operations
Section titled “Common operations”s = " Hello, Kiwi! "
println s.size() # 16println s.trim() # Hello, Kiwi!println s.lowercase() # hello, kiwi!println s.contains("Kiwi") # trueprintln s.trim().begins_with("He") # trueprintln s.trim().ends_with("!") # trueprintln s.index("Kiwi") # 9println s.replace("Kiwi", "World") # Hello, World!println "a,b,c".split(",") # ["a", "b", "c"]println ["a", "b"].join("-") # a-bprintln "ab" * 3 # abababprintln "abc".chars() # ["a", "b", "c"]Concatenate with +. If one side is not a string, it is converted:
println "Total: " + 42 # Total: 42Compare strings with ==, < and >. Ordering follows the current culture’s alphabetical rules rather than raw character codes.
The Built-in Methods reference lists every string method.
Regular expressions
Section titled “Regular expressions”Several built-in methods take a regular expression. Write patterns as raw strings so backslashes reach the regex engine untouched.
text = "Order 66 shipped on 2024-06-15"
println text.matches('.*\d{4}-\d{2}-\d{2}') # trueprintln text.find('\d+') # 66println text.scan('\d+') # ["66", "2024", "06", "15"]println text.match('(\d{4})-(\d{2})-(\d{2})') # ["2024", "06", "15"]println text.rreplace('\d', "#") # Order ## shipped on ####-##-##println "a1b22c".rsplit('\d+') # ["a", "b", "c"]| Method | Returns |
|---|---|
.matches(re) | Whether the whole string matches |
.matches_all(re) | Whether the pattern matches anywhere |
.find(re) | The first match, or "" |
.match(re) | The capture groups of the first match |
.scan(re) | Every match |
.scan_groups(re) | The capture groups of every match |
.named_captures(re) | A hashmap of named groups from the first match |
.rreplace(re, replacement) | Every match replaced; $1 refers to a group |
.rsplit(re, limit?) | The string split on the pattern |
.regex_escape() | The string with regex metacharacters escaped |
.replace and .split treat their argument as plain text, not a pattern. For more, see the regex package.
The string package
Section titled “The string package”The string package adds formatting helpers. Each one can be called as a package function or as a method on any string:
println "the quick fox".titleize() # The Quick Foxprintln "hELLO".capitalize() # Helloprintln " too many spaces ".squish() # too many spacesprintln "42".padstart(6, "0") # 000042println "hi".center(6, "*") # **hi**println string::slug("Hello, World!") # hello-worldTo format with % specifiers like printf, use fmt. For {{placeholder}} templates, use template.
.to_bytes() encodes a string as UTF-8 bytes, and .to_string() on bytes decodes them. .ord() returns the code point of the first character.
println "hi".to_bytes() # [104, 105]println "hi".to_bytes().to_string() # hiprintln "A".ord() # 65