Lcl · Core · Packages · Libraries
StringByte-oriented string operations.
All functions treat strings as byte sequences; indices are 0-based
byte offsets. String operands must actually be strings: passing
a number, list, or other value is an error, because string operators
never render their arguments implicitly. Use String::from
to render a value explicitly – it (and join’s list
elements, which exist to be rendered) are the deliberate exceptions that
accept any value.
Examples:
>> String::upper [String::from 3.14]
"3.14"
>> catch { String::upper 3.14 }
1String::from vReturn the canonical string form of any value.
The explicit counterpart to interpolation’s type-preserving rule: a list and its canonical string print identically but are different values; this converts for real.
Examples:
>> String::from (1 2 3)
"1 2 3"
>> type [String::from 42]
"string"String::eq? a bReturn 1 if a and b are the same bytes,
else 0.
== treats numeric text numerically ("007"
equals 7); this is the bytewise comparison for when
spelling matters.
Examples:
>> String::eq? "007" "7"
0
>> == "007" "7"
1
>> String::eq? "abc" "abc"
1String::upper sReturn s uppercased (ASCII).
Examples:
>> String::upper "abc"
"ABC"String::lower sReturn s lowercased (ASCII).
Examples:
>> String::lower "ABC"
"abc"String::find s subReturn the byte index of the first occurrence of sub in
s, or -1 if absent.
Examples:
>> String::find "banana" "na"
2
>> String::find "banana" "zz"
-1String::replace s old newReturn s with every occurrence of old
replaced by new.
Examples:
>> String::replace "banana" "na" "NA"
"baNANA"String::split s (chars {})Split s into a list of strings.
chars is a set of separator bytes:
s is split at every byte that appears in
chars, and empty fields between adjacent separators are
kept. With no chars, every byte becomes its own
element.
Examples:
>> String::split "a,b;c" ",;"
("a" "b" "c")
>> String::split "a,b,,c" ","
("a" "b" "" "c")
>> String::split "abc"
("a" "b" "c")String::join list (sep " ")Join the elements of list into one string, separated by
sep (default: a single space).
Examples:
>> String::join (a b c)
"a b c"
>> String::join (a b c) ", "
"a, b, c"String::length sReturn the length of s in bytes.
Examples:
>> String::length "abc"
3
>> String::length ""
0String::index s iReturn the byte at index i as a one-character
string.
Examples:
>> String::index "banana" 1
"a"String::range s start endReturn the substring of s from byte start
(inclusive) to byte end (exclusive).
Examples:
>> String::range "banana" 1 4
"ana"String::trim sReturn s with leading and trailing whitespace
removed.
Examples:
>> String::trim " hi "
"hi"