Reference · Strings
trim
signature
trim(str) -> strWhat it does
The text without the whitespace at its start and end: spaces, tabs, newlines and carriage returns, and the rest of Unicode's White_Space as well (U+0085 too, but not U+FEFF, a byte-order mark), the same in the browser. Whitespace inside the text stays.
Examples
print(trim(" hi "))print(trim("\t\nhi\r\n") == "hi")print(trim(" a b "))output
hitruea bEdge cases
- The empty string, and a string of only whitespace, give the empty string.
- Inner runs of spaces are kept:
trimis not a squeeze. - A no-break space and an em space are whitespace too, and trimmed.
- Only whitespace: a dash or a quote at the edge stays, and so does a byte-order mark
(
len(trim("\uFEFFa"))is 2).
print(trim(""), trim(" "), len(trim(" ")))print(trim(" inner spaces "))print(trim(" hi ") | len, trim(" x "))print(trim("- x -"))output
0inner spaces2 x- x -Errors
An argument that is not a string, null included, is a runtime error, not an error
value. A field that may be null gets ?? "" first.
this one fails
print(trim(null))output
line 1: `trim` expects a str as argument 1, got nulllet note = nullprint(trim(note ?? "") == "")output
trueChaining
print(" Hi " | trim | lower)print(" a , b " | split(",") | map(trim))print((" " | trim | len) == 0)output
hi["a", "b"]trueSee also
split, lower, upper, replace, len
Thread
Thread 0 posts
On trim(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo