Module std::string

String conversion and manipulation utilities.

Most string operations (.find(), .slice(), .trim(), etc.) are built-in methods available on all strings. This module provides type-conversion helpers and additional string utilities.

Examples

import std::string;

fn main() {
    let s = string.from_int(42);        // "42"
    let n = string.to_int("42");        // 42
    let nl = string.from_char(10);      // "\n"
    let stars = string.repeat("*", 3);  // "***"
    let padded = string.pad_left("7", 3, "0");  // "007"
}

Contents

Functions

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.

Unlike C's atoi, this function does not stop at the first non-digit character — any non-digit anywhere in the string (after an optional leading +/-) causes the whole parse to fail and return 0. For example, "42abc" returns 0, not 42.

There is no overflow protection: very large inputs will wrap silently.

Examples

let n = string.to_int("42");     // 42
let m = string.to_int("42abc");  // 0 — partial numbers return 0
let z = string.to_int("");       // 0 — empty string returns 0

Function digit_value

fn digit_value(ch: String) -> i32

Return the numeric value of a single-character digit string, or -1 if not a digit.

Function is_empty

pub fn is_empty(s: String) -> bool

Check if a string is empty.

Examples

string.is_empty("")    // true
string.is_empty("hi")  // false

Function repeat

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

Repeat a string n times.

Examples

let stars = string.repeat("*", 5);  // "*****"

Function pad_left

pub fn pad_left(s: String, width: i32, pad: String) -> String

Pad a string on the left to reach the given width.

If s is already at least width characters, returns s unchanged.

Examples

string.pad_left("42", 5, " ")   // "   42"
string.pad_left("42", 5, "0")   // "00042"

Function pad_right

pub fn pad_right(s: String, width: i32, pad: String) -> String

Pad a string on the right to reach the given width.

If s is already at least width characters, returns s unchanged.

Examples

string.pad_right("hi", 5, " ")  // "hi   "

Function is_numeric

pub fn is_numeric(s: String) -> bool

Check if a string contains only ASCII digits (0-9).

Returns false for empty strings.

Examples

string.is_numeric("123")   // true
string.is_numeric("12a")   // false
string.is_numeric("")      // false

Function count

pub fn count(haystack: String, needle: String) -> i32

Count the number of non-overlapping occurrences of needle in haystack.

Examples

string.count("abcabc", "abc")  // 2
string.count("hello", "x")     // 0

Function starts_with

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

Check if a string starts with the given prefix.

Examples

string.starts_with("hello", "he")   // true
string.starts_with("hello", "lo")   // false

Function ends_with

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

Check if a string ends with the given suffix.

Examples

string.ends_with("hello", "lo")   // true
string.ends_with("hello", "he")   // false

Function contains

pub fn contains(s: String, sub: String) -> bool

Check if a string contains the given substring.

Examples

string.contains("hello world", "world")  // true
string.contains("hello", "xyz")          // false

Function is_ascii

pub fn is_ascii(s: String) -> bool

Check if a string contains only ASCII characters (code points 0–127).

Examples

string.is_ascii("hello")  // true
string.is_ascii("héllo")  // false

Function to_lower

pub fn to_lower(s: String) -> String

Convert a string to lowercase.

Function to_upper

pub fn to_upper(s: String) -> String

Convert a string to uppercase.

Function trim

pub fn trim(s: String) -> String

Trim leading and trailing whitespace from a string.

Function replace

pub fn replace(s: String, old: String, new_val: String) -> String

Replace all occurrences of old in s with new_val.

Function split

pub fn split(s: String, sep: String) -> Vec<String>

Split a string by sep into a list of substrings.

Examples

import std::string;

let parts = string.split("a,b,c", ",");  // ["a", "b", "c"]

Function lines

pub fn lines(s: String) -> Vec<String>

Split a string into lines, stripping \r\n or \n endings.

Examples

import std::string;

let lines = string.lines("foo\nbar");  // ["foo", "bar"]

Function join

pub fn join(parts: Vec<String>, sep: String) -> String

Join a list of strings with sep between each element.

Examples

import std::string;

let s = string.join(parts, ", ");

Traits

Trait ToString

Trait for types that can be converted to a string representation.

Provides a standard interface for string conversion across modules.

Examples

import std::string;

// Implement for custom types:
// impl ToString for MyType {
//     fn to_str(self: MyType) -> String { ... }
// }

Methods

fn to_str(self: Self) -> String