String Library (Str)
Linked to the str type.
Query
Indexes and lengths count bytes (UTF-8): len, at, index_of, and substring. For text with accents, emoji, or other non-ASCII characters, the by-character functions count characters instead.
Str::len(val: str) -> int
The length in bytes. char_len counts characters.
Str::contains(val: str, seq: str) -> bool
#[main]
fn main() {
const val = "hello, world";
pln(val.contains(", w"));
}Str::starts_with(val: str, seq: str) -> bool
#[main]
fn main() {
const val = "hello";
pln(val.starts_with("he"));
}Str::ends_with(val: str, seq: str) -> bool
#[main]
fn main() {
const val = "hello";
pln(val.ends_with("llo"));
}Str::index_of(val: str, seq: str) -> int
The byte index, or -1 if not found.
#[main]
fn main() {
const val = "hello, world";
pln(val.index_of(", w"));
}Access
Str::at(val: str, index: int) -> str
Clamps to the last character if the index is out of bounds, rather than erroring.
Str::first(val: str) -> str
Str::last(val: str) -> str
Str::substring(val: str, start: int = 0, end: int = -1) -> str
[start, end) — default is the whole string.
#[main]
fn main() {
const val = "hello, world";
pln(val.substring(7), ' ', val.substring(3, 8));
}By Character
These count characters (Unicode scalar values), not bytes, so 'héllo' has 5 characters and 6 bytes.
Str::char_len(val: str) -> int
#[main]
fn main() {
pln("héllo".char_len(), ' ', "héllo".len());
}Str::chars(val: str) -> list
Each character as a string.
Str::char_at(val: str, index: int) -> str
A negative index counts from the end. Past either end gives ''.
#[main]
fn main() {
const val = "héllo";
pln(val.char_at(1), ' ', val.char_at(-1));
}Str::char_index_of(val: str, seq: str) -> int
The character index, or -1 if not found.
#[main]
fn main() {
const val = "héllo, world";
pln(val.char_index_of("world"), ' ', val.index_of("world"));
}Str::char_substring(val: str, start: int = 0, end: int = -1) -> str
[start, end) in characters. Default is the whole string.
#[main]
fn main() {
const val = "héllo, wörld";
pln(val.char_substring(7), ' ', val.char_substring(1, 4));
}Case & Whitespace
Str::upper(val: str) -> str
Str::lower(val: str) -> str
Str::trim(val: str) -> str
Strips newlines, tabs, and spaces from both ends.
Str::trim_start(val: str) -> str
#[main]
fn main() {
const val = "\n\thello\t\n";
pln(val.trim_start());
}Str::trim_end(val: str) -> str
#[main]
fn main() {
const val = "\n\thello\t\n";
pln(val.trim_end());
}Modify
Str::push(val: str, other: str) -> void
Mutates val in place — other is left untouched.
#[main]
fn main() {
const val = "hello";
val.push(", world");
pln(val);
}Str::replace(val: str, find: str, replace: str = "") -> str
Returns a new string — the original is unmodified. Default replace just removes every occurrence of find.
#[main]
fn main() {
const val = "hello john";
pln(val.replace(" ", ", "));
}Str::split(val: str, sep: str = " ") -> list
#[main]
fn main() {
const val = "hello, world";
pln(val.split(", "));
}Regex
Str::matches(val: str, regex: str) -> bool
#[main]
fn main() {
const val = "I categorically deny having triskaidekaphobia.";
const regex = "\\b\\w{13}\\b";
pln(val.matches(regex));
}Str::find_matches(val: str, regex: str) -> list
Every match, as (content, start, end) tuples.
#[main]
fn main() {
const val = "I categorically deny having triskaidekaphobia.";
const regex = "\\b\\w{13}\\b";
pln(val.find_matches(regex));
}