Skip to content

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.

FunctionDescription
toUpperCaseUpper-cases text.
toLowerCaseLower-cases text.
trimRemoves leading and trailing whitespace.
trimStartRemoves leading whitespace.
trimEndRemoves trailing whitespace.
startsWithWhether text starts with a prefix.
endsWithWhether text ends with a suffix.
includesWhether text contains a substring, or a list contains a value.
indexOfPosition of the first match, or -1.
lastIndexOfPosition of the last match, or -1.
sliceA section of text or a list; negative positions count from the end.
splitSplits text on a separator.
replaceReplaces the first occurrence of some text (no patterns).
replaceAllReplaces every occurrence of some text (no patterns).
padStartPads text at the start to a length.
padEndPads text at the end to a length.
repeatRepeats text a number of times.
atThe item or character at a position; negative positions count from the end.
matchesWhether text contains a match for a regular expression (JavaScript syntax without backreferences or lookaround, linear time; anchor with ^ and $; prefix (?i) to ignore case).
toStringRenders a value as text, as a template would.
toNumberParses 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
bonsai
"hello".toUpperCase() // => "HELLO"

toLowerCase ​

Lower-cases text.

  • toLowerCase(string): string
bonsai
toLowerCase("Hello") // => "hello"

trim ​

Removes leading and trailing whitespace.

  • trim(string): string
bonsai
"  hi  ".trim() // => "hi"

trimStart ​

Removes leading whitespace.

  • trimStart(string): string
bonsai
"  hi  ".trimStart() // => "hi  "

trimEnd ​

Removes trailing whitespace.

  • trimEnd(string): string
bonsai
"  hi  ".trimEnd() // => "  hi"

startsWith ​

Whether text starts with a prefix.

  • startsWith(string, string): boolean
bonsai
"invoice-42".startsWith("invoice-") // => true

endsWith ​

Whether text ends with a suffix.

  • endsWith(string, string): boolean
bonsai
"report.pdf".endsWith(".pdf") // => true

includes ​

Whether text contains a substring, or a list contains a value.

  • includes(string, string): boolean
bonsai
"hello world".includes("lo w") // => true

indexOf ​

Position of the first match, or -1.

  • indexOf(string, string): number
bonsai
"banana".indexOf("an") // => 1
"banana".indexOf("x") // => -1

lastIndexOf ​

Position of the last match, or -1.

  • lastIndexOf(string, string): number
bonsai
"banana".lastIndexOf("an") // => 3

slice ​

A section of text or a list; negative positions count from the end.

  • slice(string, number, number?): string
bonsai
"hello".slice(1, 3) // => "el"
"hello".slice(-3) // => "llo"

split ​

Splits text on a separator.

  • split(string, string, number?): string[]
bonsai
"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
bonsai
"a-b-c".replace("-", "+") // => "a+b-c"

replaceAll ​

Replaces every occurrence of some text (no patterns).

  • replaceAll(string, string, string): string
bonsai
"a-b-c".replaceAll("-", "+") // => "a+b+c"

padStart ​

Pads text at the start to a length.

  • padStart(string, number, string?): string
bonsai
"7".padStart(3, "0") // => "007"

padEnd ​

Pads text at the end to a length.

  • padEnd(string, number, string?): string
bonsai
"ab".padEnd(4, ".") // => "ab.."

repeat ​

Repeats text a number of times.

  • repeat(string, number): string
bonsai
"ab".repeat(3) // => "ababab"

at ​

The item or character at a position; negative positions count from the end.

  • at(string, number): string | null
bonsai
"hello".at(-1) // => "o"
"hello".at(10) // => null

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).

  • matches(string, string): boolean
bonsai
"INV-2041".matches("^INV-[0-9]+$") // => true
"Hello".matches("(?i)^hello$") // => true
"a.b".matches("a\\.b") // => true

toString ​

Renders a value as text, as a template would.

  • toString(string | number | boolean | null | timestamp | duration): string
bonsai
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
bonsai
"42.5".toNumber() // => 42.5
" -1e3 ".toNumber() // => -1000
try("0x10".toNumber(), 0) // => 0