docs / string

string

Functions

at

fn(s: Str, index: Int) -> Option<Str>

compare

fn(a: Str, b: Str) -> Cmp

contains

fn(s: Str, needle: Str) -> Bool

Check if s contains needle.

test.assert(string.contains("hello world", "lo wo"));
test.assert(!string.contains("hello world", "xyz"));

ends_with

fn(s: Str, suffix: Str) -> Bool

Check if s ends with the given suffix.

test.assert(string.ends_with("hello world", "world"));
test.assert(!string.ends_with("hello", "world"));

index_of

fn(s: Str, needle: Str) -> Option<Int>

Find the first occurrence of needle in s. Returns None if not found.

import core/option { Some, None }
test.assert_eq(string.index_of("hello world", "world"), Some(6));
test.assert_eq(string.index_of("hello world", "xyz"), None);

is_empty

fn(s: Str) -> Bool

Check if a string is empty.

test.assert(string.is_empty(""));
test.assert(!string.is_empty("a"));

last_index_of

fn(s: Str, needle: Str) -> Option<Int>

Find the last occurrence of needle in s. Returns None if not found. Searches forward, tracking the last found position.

length

fn(s: Str) -> Int

replace

fn(s: Str, needle: Str, replacement: Str) -> Str

Replace all occurrences of needle with replacement.

slice

fn(s: Str, start: Int, end: Int) -> Str

Extract a substring from start (inclusive) to end (exclusive).

test.assert_eq(string.slice("hello world", 0, 5), "hello");
test.assert_eq(string.slice("hello world", 6, 11), "world");

split

fn(s: Str, separator: Str) -> Array<Str>

Split a string by a separator. Returns Array<Str>.

test.assert_eq(string.split("a,b,c", ","), ["a", "b", "c"]);
test.assert_eq(string.split("hello", ","), ["hello"]);

starts_with

fn(s: Str, prefix: Str) -> Bool

Check if s starts with the given prefix.

test.assert(string.starts_with("hello world", "hello"));
test.assert(!string.starts_with("hello", "world"));

trim

fn(s: Str) -> Str

Trim whitespace from both ends of a string.

test.assert_eq(string.trim("  hello  "), "hello");
test.assert_eq(string.trim("   "), "");