String API

Functions for querying and manipulating strings. Zym strings are UTF-8 encoded and immutable — all operations return new strings.

Shared Functions

These functions work with both strings and lists. See also List API.

length(str)

Returns the number of characters (Unicode code points), not the byte length.

length("hello")    // 5
length("")          // 0
length("café")      // 4
indexOf(str, search)

Returns the byte offset of the first occurrence of a substring, or -1 if not found.

indexOf("hello world", "world")   // 6
indexOf("hello world", "xyz")     // -1
contains(str, search)

Checks whether a string contains a substring. Returns true or false.

contains("hello world", "world")   // true
contains("hello world", "xyz")     // false
slice(str, start, end)

Extracts a substring. Range is [start, end) — character-based indices. Negative indices count from the end.

slice("hello world", 0, 5)    // "hello"
slice("hello world", 6, 11)   // "world"
slice("hello", -3, 5)         // "llo"
concat(str1, str2)

Concatenates two strings. Max result size is 4095 bytes.

concat("hello", " world")   // "hello world"

Character Access

charAt(str, index)

Returns the character at a given index as a single-character string.

charAt("hello", 0)    // "h"
charAt("café", 3)     // "é"
charCodeAt(str, index)

Returns the Unicode code point of the character at a given index.

charCodeAt("A", 0)       // 65
charCodeAt("hello", 1)   // 101
fromCodePoint(codepoint)

Creates a single-character string from a Unicode code point (0–1114111).

fromCodePoint(65)      // "A"
fromCodePoint(9829)    // "♥"
byteLength(str)

Returns the byte length of a string (UTF-8 encoded size).

byteLength("hello")   // 5
byteLength("café")    // 5 (é is 2 bytes)

Search & Match

startsWith(str, prefix)

Checks whether a string starts with a given prefix.

startsWith("hello world", "hello")   // true
startsWith("hello world", "world")   // false
endsWith(str, suffix)

Checks whether a string ends with a given suffix.

endsWith("hello world", "world")   // true
endsWith("hello world", "hello")   // false
lastIndexOf(str, search)

Returns the byte offset of the last occurrence of a substring, or -1.

lastIndexOf("abcabc", "bc")    // 4
lastIndexOf("hello", "xyz")    // -1

Case Conversion

toUpperCase(str)

Converts all ASCII characters to uppercase.

toUpperCase("hello")    // "HELLO"
toLowerCase(str)

Converts all ASCII characters to lowercase.

toLowerCase("HELLO")    // "hello"

Trimming

trim(str)

Removes leading and trailing whitespace.

trim("  hello  ")    // "hello"
trimStart(str)

Removes leading whitespace.

trimStart("  hello  ")    // "hello  "
trimEnd(str)

Removes trailing whitespace.

trimEnd("  hello  ")    // "  hello"

Replacement

replace(str, search, replacement)

Replaces the first occurrence of a substring.

replace("hello world", "world", "Zym")    // "hello Zym"
replace("aaa", "a", "b")                  // "baa"
replaceAll(str, search, replacement)

Replaces all occurrences of a substring.

replaceAll("aaa", "a", "b")    // "bbb"

Splitting & Joining

split(str, delimiter)

Splits a string into a list of substrings using a delimiter.

split("a,b,c", ",")         // ["a", "b", "c"]
split("hello world", " ")   // ["hello", "world"]
split("abc", "")             // ["a", "b", "c"]
substr(str, start, end)

Extracts a substring using character indices. Range is [start, end).

substr("hello world", 0, 5)    // "hello"
substr("hello world", 6, 11)   // "world"

Padding & Repetition

padStart(str, targetLen, pad)

Pads a string from the start to reach a target length.

padStart("5", 3, "0")       // "005"
padStart("hi", 5, " ")      // "   hi"
padStart("hello", 3, "x")   // "hello" (already long enough)
padEnd(str, targetLen, pad)

Pads a string from the end to reach a target length.

padEnd("hi", 5, ".")       // "hi..."
padEnd("hello", 3, "x")    // "hello" (already long enough)
repeat(str, count)

Repeats a string a specified number of times.

repeat("ha", 3)      // "hahaha"
repeat("-", 10)      // "----------"
repeat("abc", 0)     // ""

See also: List APIConversions APIMath API