Module string

String manipulation utilities.

Functions for searching, slicing, transforming, and converting strings. These supplement the built-in string operations.

Examples

import std::string;

fn main() {
    let s = "  Hello, World!  ";
    let trimmed = string.trim(s);
    let lower = string.to_lower(trimmed);
    println(lower);  // "hello, world!"
}

Contents

Functions

Function find

pub fn find(haystack: string, needle: string) -> i32

Find the first occurrence of needle in haystack.

Returns the byte offset, or -1 if not found.

Examples

let pos = string.find("hello world", "world");  // 6

Function slice

pub fn slice(s: string, start: i32, end: i32) -> string

Extract a substring by byte indices [start, end).

Examples

let s = string.slice("hello", 1, 4);  // "ell"

Function trim

pub fn trim(s: string) -> string

Strip leading and trailing whitespace.

Examples

let s = string.trim("  hello  ");  // "hello"

Function replace

pub fn replace(s: string, from: string, to: string) -> string

Replace all occurrences of from with to in the input string.

Examples

let r = string.replace("aabbcc", "bb", "xx");  // "aaxxcc"

Function split

pub fn split(s: string, delimiter: string) -> string

Split a string by a delimiter.

Returns the substrings joined by newlines (the runtime does not yet support returning arrays from FFI).

Function to_lower

pub fn to_lower(s: string) -> string

Convert a string to lowercase.

Examples

let s = string.to_lower("HELLO");  // "hello"

Function to_upper

pub fn to_upper(s: string) -> string

Convert a string to uppercase.

Function char_at

pub fn char_at(s: string, index: i32) -> i32

Returns the character code at the given byte index.

Function repeat

pub fn repeat(s: string, n: i32) -> string

Repeat a string n times.

Examples

let s = string.repeat("ab", 3);  // "ababab"

Function index_of

pub fn index_of(haystack: string, needle: string, start_index: i32) -> i32

Find the first occurrence of needle starting from start_index.

Returns the byte offset, or -1 if not found.

Function starts_with

pub fn starts_with(s: string, prefix: string) -> bool

Test whether s starts with the given prefix.

Function ends_with

pub fn ends_with(s: string, suffix: string) -> bool

Test whether s ends with the given suffix.

Function contains

pub fn contains(haystack: string, needle: string) -> bool

Test whether haystack contains needle.

Function from_int

pub fn from_int(n: i32) -> string

Convert an integer to its string representation.

Examples

let s = string.from_int(42);  // "42"

Function from_float

pub fn from_float(f: f64) -> string

Convert a float to its string representation.

Function from_bool

pub fn from_bool(b: bool) -> string

Convert a bool to "true" or "false".

Function from_char

pub fn from_char(code: i32) -> string

Convert a character code to a single-character string.

Examples

let newline = string.from_char(10);

Function to_int

pub fn to_int(s: string) -> i32

Parse a string as an integer. Returns 0 if parsing fails.

Examples

let n = string.to_int("42");  // 42