Skip to main content

String Library (Str)

Linked to the str type.

Query

Str.len(val: str) -> int

str-len.stof
#[main]
fn main() {
  pln("hello".len());
}
Output

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

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

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

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

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

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

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

-1 if not found.

str-index-of.stof
#[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.stof
#[main]
fn main() {
  const val = "hello";
  pln(val[1]);
}
Output

Str.first(val: str) -> str

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

Str.last(val: str) -> str

str-last.stof
#[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.stof
#[main]
fn main() {
  const val = "hello, world";
  pln(val.substring(7), val.substring(3, 8));
}
Output

Case & Whitespace

Str.upper(val: str) -> str

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

Str.lower(val: str) -> str

str-lower.stof
#[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.stof
#[main]
fn main() {
  const val = "\n\thello\t\n";
  pln(val.trim());
}
Output

Str.trim_start(val: str) -> str

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

Str.trim_end(val: str) -> str

str-trim-end.stof
#[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.stof
#[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.stof
#[main]
fn main() {
  const val = "hello john";
  pln(val.replace(" ", ", "));
}
Output

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

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

Regex

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

str-matches.stof
#[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.stof
#[main]
fn main() {
  const val = "I categorically deny having triskaidekaphobia.";
  const regex = "\\b\\w{13}\\b";
  pln(val.find_matches(regex));
}
Output