Skip to main content

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-len.stofPlayground ↗
#[main]
fn main() {
  pln("hello".len());
}
Output

Str::contains(val: str, seq: str) -> bool​

str-contains.stofPlayground ↗
#[main]
fn main() {
  const val = "hello, world";
  pln(val.contains(", w"));
}
Output

Str::starts_with(val: str, seq: str) -> bool​

str-starts-with.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val.starts_with("he"));
}
Output

Str::ends_with(val: str, seq: str) -> bool​

str-ends-with.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val.ends_with("llo"));
}
Output

Str::index_of(val: str, seq: str) -> int​

The byte index, or -1 if not found.

str-index-of.stofPlayground ↗
#[main]
fn main() {
  const val = "hello, world";
  pln(val.index_of(", w"));
}
Output

Access​

Str::at(val: str, index: int) -> str​

Clamps to the last character if the index is out of bounds, rather than erroring.

str-at.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val[1]);
}
Output

Str::first(val: str) -> str​

str-first.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val.first());
}
Output

Str::last(val: str) -> str​

str-last.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val.last());
}
Output

Str::substring(val: str, start: int = 0, end: int = -1) -> str​

[start, end) — default is the whole string.

str-substring.stofPlayground ↗
#[main]
fn main() {
  const val = "hello, world";
  pln(val.substring(7), ' ', val.substring(3, 8));
}
Output

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​

str-char-len.stofPlayground ↗
#[main]
fn main() {
  pln("héllo".char_len(), ' ', "héllo".len());
}
Output

Str::chars(val: str) -> list​

Each character as a string.

str-chars.stofPlayground ↗
#[main]
fn main() {
  pln("日本語".chars());
}
Output

Str::char_at(val: str, index: int) -> str​

A negative index counts from the end. Past either end gives ''.

str-char-at.stofPlayground ↗
#[main]
fn main() {
  const val = "héllo";
  pln(val.char_at(1), ' ', val.char_at(-1));
}
Output

Str::char_index_of(val: str, seq: str) -> int​

The character index, or -1 if not found.

str-char-index-of.stofPlayground ↗
#[main]
fn main() {
  const val = "héllo, world";
  pln(val.char_index_of("world"), ' ', val.index_of("world"));
}
Output

Str::char_substring(val: str, start: int = 0, end: int = -1) -> str​

[start, end) in characters. Default is the whole string.

str-char-substring.stofPlayground ↗
#[main]
fn main() {
  const val = "héllo, wörld";
  pln(val.char_substring(7), ' ', val.char_substring(1, 4));
}
Output

Case & Whitespace​

Str::upper(val: str) -> str​

str-upper.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  pln(val.upper());
}
Output

Str::lower(val: str) -> str​

str-lower.stofPlayground ↗
#[main]
fn main() {
  const val = "HELLO";
  pln(val.lower());
}
Output

Str::trim(val: str) -> str​

Strips newlines, tabs, and spaces from both ends.

str-trim.stofPlayground ↗
#[main]
fn main() {
  const val = "\n\thello\t\n";
  pln(val.trim());
}
Output

Str::trim_start(val: str) -> str​

str-trim-start.stofPlayground ↗
#[main]
fn main() {
  const val = "\n\thello\t\n";
  pln(val.trim_start());
}
Output

Str::trim_end(val: str) -> str​

str-trim-end.stofPlayground ↗
#[main]
fn main() {
  const val = "\n\thello\t\n";
  pln(val.trim_end());
}
Output

Modify​

Str::push(val: str, other: str) -> void​

Mutates val in place — other is left untouched.

str-push.stofPlayground ↗
#[main]
fn main() {
  const val = "hello";
  val.push(", world");
  pln(val);
}
Output

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.

str-replace.stofPlayground ↗
#[main]
fn main() {
  const val = "hello john";
  pln(val.replace(" ", ", "));
}
Output

Str::split(val: str, sep: str = " ") -> list​

str-split.stofPlayground ↗
#[main]
fn main() {
  const val = "hello, world";
  pln(val.split(", "));
}
Output

Regex​

Str::matches(val: str, regex: str) -> bool​

str-matches.stofPlayground ↗
#[main]
fn main() {
  const val = "I categorically deny having triskaidekaphobia.";
  const regex = "\\b\\w{13}\\b";
  pln(val.matches(regex));
}
Output

Str::find_matches(val: str, regex: str) -> list​

Every match, as (content, start, end) tuples.

str-find-matches.stofPlayground ↗
#[main]
fn main() {
  const val = "I categorically deny having triskaidekaphobia.";
  const regex = "\\b\\w{13}\\b";
  pln(val.find_matches(regex));
}
Output