Skip to content

@strings

Import this module with import @strings.

Function Signature Description
to_upper (s string) -> string Convert to uppercase
to_lower (s string) -> string Convert to lowercase
to_title (s string) -> string Capitalize the first letter of each whitespace-separated word, lowercase the rest
to_snake_case (s string) -> string Convert camelCase, PascalCase, spaces, and hyphens to snake_case; acronym runs stay together (HTTPServer → http_server)
to_camel_case (s string) -> string Convert snake_case, spaces, and hyphens to camelCase
to_kebab_case (s string) -> string Convert camelCase, PascalCase, spaces, and underscores to kebab-case (same word boundaries as to_snake_case)
to_pascal_case (s string) -> string Convert snake_case, spaces, and hyphens to PascalCase (like to_camel_case, but the first word is capitalized too)
to_screaming_snake_case (s string) -> string to_snake_case followed by to_upper (SCREAMING_SNAKE_CASE)
capitalize (s string) -> string Uppercase the first character, leave the rest untouched; "" returns ""
Function Signature Description
char_at (s string, index i64) -> char Character at byte index; panics if out of bounds

Each returns a new string (string is an immutable value type). index is a byte offset, matching char_at. A char argument is a codepoint and is UTF-8 encoded into the result.

Function Signature Description
append_char (s string, c char) -> string New string with c added at the end
prepend_char (s string, c char) -> string New string with c added at the front
insert_char_at (s string, index i64, c char) -> string New string with c inserted at byte index; index == len appends; panics if index < 0 or index > len
remove_at (s string, index i64) -> string New string with the byte at index removed; panics if out of bounds
set_char_at (s string, index i64, c char) -> string New string with the byte at index replaced by c; panics if out of bounds
Function Signature Description
is_empty (s string) -> bool Check if string is empty (length zero; does not trim)
contains (s string, sub string) -> bool Check if contains substring
starts_with (s string, prefix string) -> bool Check prefix
ends_with (s string, suffix string) -> bool Check suffix
index_of (s string, sub string) -> i64 First index of substring
last_index_of (s string, sub string) -> i64 Last index of substring (-1 if not found)
count (s string, sub string) -> i64 Count occurrences
contains_any (s string, chars string) -> bool True if any single character from chars appears in s; false when chars is empty
equal_fold (a string, b string) -> bool Case-insensitive equality (ASCII)
compare (a string, b string) -> i64 Bytewise comparison; -1 if a sorts first, 1 if b does, 0 if equal
Function Signature Description
is_alpha (c char) -> bool True if c is an ASCII letter (a-z, A-Z)
is_digit (c char) -> bool True if c is a decimal digit (0-9)
is_alnum (c char) -> bool True if c is a letter or digit
is_whitespace (c char) -> bool True if c is space, tab, newline, or carriage return
is_upper (c char) -> bool True if c is an uppercase letter (A-Z)
is_lower (c char) -> bool True if c is a lowercase letter (a-z)
Function Signature Description
trim (s string) -> string Trim whitespace
trim_left (s string) -> string Trim left whitespace
trim_right (s string) -> string Trim right whitespace
remove_prefix (s string, prefix string) -> string Remove prefix if present, otherwise return unchanged
remove_suffix (s string, suffix string) -> string Remove suffix if present, otherwise return unchanged
replace (s string, old string, new string) -> string Replace all occurrences
repeat (s string, count i64) -> string Repeat string
reverse (s string) -> string Reverse string
truncate (s string, max i64, ellipsis string) -> string Return s unchanged if len(s) <= max, else the first max - len(ellipsis) bytes plus ellipsis (total byte length exactly max); panics if max < len(ellipsis)
Function Signature Description
split (s string, sep string) -> [string] Split into array
split_whitespace (s string) -> [string] Split on runs of whitespace, discarding empty pieces
split_n (s string, sep string, n i64) -> [string] Split into at most n pieces; the last holds the unsplit remainder. Empty array when n <= 0
join (arr [string], sep string) -> string Join array
slice (s string, start i64, end i64) -> string Extract substring
to_chars (s string) -> [char] Convert string to char array
from_chars (chars [char]) -> string Convert char array to string

string is an immutable value type, so building one with repeated += reallocates and copies the whole accumulated content each time — assembling N pieces is O(N²). Builder is a growable byte accumulator that appends in place and is finalized to a string once, making the same work amortized O(M) in the total byte count M.

Builder is arena-managed like any struct: it goes out of scope with its enclosing arena, with no manual free. The buffer grows by doubling. strings.builder_clear keeps the allocated capacity, so a builder can be reused across frames of a loop without reallocating.

Every operation on a Builder mutates it, so it must be declared mut — a const builder is a compile error (E3062). The builder_append* calls take the builder by reference with no & at the call site, but — like an array — a function that appends to a builder passed in by the caller must take it as &b Builder; appending through a plain (immutable) parameter is a compile error (E5007).

Function Signature Description
builder () -> Builder Create an empty builder
builder_reserve (b Builder, n i64) -> void Grow the buffer to hold at least n bytes; a negative n is ignored
builder_append (b Builder, s string) -> void Append the bytes of s
builder_append_char (b Builder, c char) -> void Append the codepoint c, UTF-8 encoded (1–4 bytes)
builder_append_bytes (b Builder, data [u8]) -> void Append every byte of data
builder_append_i64 (b Builder, n i64) -> void Append the decimal text of n
builder_append_line (b Builder, s string) -> void Append s followed by a newline
builder_len (b Builder) -> i64 Bytes accumulated so far
builder_clear (b Builder) -> void Reset length to zero, keeping capacity
build (b Builder) -> string Copy the accumulated bytes into a new string; the builder stays usable
import @strings
do append_row(&b Builder, row [string]) {
strings.builder_append_line(b, strings.join(row, ","))
}
do rows_to_csv(rows [[string]]) -> string {
mut b Builder = strings.builder()
for_each row in rows {
append_row(b, row)
}
return strings.build(b)
}
do main() {
}