Reference · Strings

trim

signature
trim(str) -> str

What 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 b

Edge cases

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 null
let note = nullprint(trim(note ?? "") == "")
output
true

Chaining

print("  Hi  " | trim | lower)print(" a , b " | split(",") | map(trim))print(("   " | trim | len) == 0)
output
hi["a", "b"]true

See also

split, lower, upper, replace, len

Thread
8Useful? Like it, or add to its thread below.

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