Text
Functions over strings. Positions and lengths count UTF-16 code units, as in JavaScript. includes, indexOf, replace, and replaceAll match literal text; matches tests a regular expression.
Every function can be called as f(x, ...) or as a method, x.f(...). In the signatures, T and U stand for any type, T[] is a list, ? marks an optional parameter, and (T, number) => boolean is a lambda parameter.
| Function | Description |
|---|---|
toUpperCase | Upper-cases text. |
toLowerCase | Lower-cases text. |
trim | Removes leading and trailing whitespace. |
trimStart | Removes leading whitespace. |
trimEnd | Removes trailing whitespace. |
startsWith | Whether text starts with a prefix. |
endsWith | Whether text ends with a suffix. |
includes | Whether text contains a substring, or a list contains a value. |
indexOf | Position of the first match, or -1. |
lastIndexOf | Position of the last match, or -1. |
slice | A section of text or a list; negative positions count from the end. |
split | Splits text on a separator. |
replace | Replaces the first occurrence of some text (no patterns). |
replaceAll | Replaces every occurrence of some text (no patterns). |
padStart | Pads text at the start to a length. |
padEnd | Pads text at the end to a length. |
repeat | Repeats text a number of times. |
at | The item or character at a position; negative positions count from the end. |
matches | Whether text contains a match for a regular expression (JavaScript syntax without backreferences or lookaround, linear time; anchor with ^ and $; prefix (?i) to ignore case). |
toString | Renders a value as text, as a template would. |
toNumber | Parses decimal text as a number, e.g. "-12.5" or "1e3" (surrounding whitespace is ignored; 0x and other bases are not accepted). |
toUpperCase
Upper-cases text.
toUpperCase(string): string
"hello".toUpperCase() // => "HELLO"toLowerCase
Lower-cases text.
toLowerCase(string): string
toLowerCase("Hello") // => "hello"trim
Removes leading and trailing whitespace.
trim(string): string
" hi ".trim() // => "hi"trimStart
Removes leading whitespace.
trimStart(string): string
" hi ".trimStart() // => "hi "trimEnd
Removes trailing whitespace.
trimEnd(string): string
" hi ".trimEnd() // => " hi"startsWith
Whether text starts with a prefix.
startsWith(string, string): boolean
"invoice-42".startsWith("invoice-") // => trueendsWith
Whether text ends with a suffix.
endsWith(string, string): boolean
"report.pdf".endsWith(".pdf") // => trueincludes
Whether text contains a substring, or a list contains a value.
includes(string, string): boolean
"hello world".includes("lo w") // => trueindexOf
Position of the first match, or -1.
indexOf(string, string): number
"banana".indexOf("an") // => 1
"banana".indexOf("x") // => -1lastIndexOf
Position of the last match, or -1.
lastIndexOf(string, string): number
"banana".lastIndexOf("an") // => 3slice
A section of text or a list; negative positions count from the end.
slice(string, number, number?): string
"hello".slice(1, 3) // => "el"
"hello".slice(-3) // => "llo"split
Splits text on a separator.
split(string, string, number?): string[]
"a,b,c".split(",") // => ["a", "b", "c"]
"a,b,c".split(",", 2) // => ["a", "b"]replace
Replaces the first occurrence of some text (no patterns).
replace(string, string, string): string
"a-b-c".replace("-", "+") // => "a+b-c"replaceAll
Replaces every occurrence of some text (no patterns).
replaceAll(string, string, string): string
"a-b-c".replaceAll("-", "+") // => "a+b+c"padStart
Pads text at the start to a length.
padStart(string, number, string?): string
"7".padStart(3, "0") // => "007"padEnd
Pads text at the end to a length.
padEnd(string, number, string?): string
"ab".padEnd(4, ".") // => "ab.."repeat
Repeats text a number of times.
repeat(string, number): string
"ab".repeat(3) // => "ababab"at
The item or character at a position; negative positions count from the end.
at(string, number): string | null
"hello".at(-1) // => "o"
"hello".at(10) // => nullmatches
Whether text contains a match for a regular expression (JavaScript syntax without backreferences or lookaround, linear time; anchor with ^ and $; prefix (?i) to ignore case).
matches(string, string): boolean
"INV-2041".matches("^INV-[0-9]+$") // => true
"Hello".matches("(?i)^hello$") // => true
"a.b".matches("a\\.b") // => truetoString
Renders a value as text, as a template would.
toString(string | number | boolean | null | timestamp | duration): string
toString(42) // => "42"
toString(null) // => ""
days(1).toString() // => "P1D"toNumber
Parses decimal text as a number, e.g. "-12.5" or "1e3" (surrounding whitespace is ignored; 0x and other bases are not accepted).
toNumber(string | number): number
"42.5".toNumber() // => 42.5
" -1e3 ".toNumber() // => -1000
try("0x10".toNumber(), 0) // => 0