std::stringstring 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.
import std::string;
fn main() {
let s = string.from_int(42); // "42"
let n = string.to_int("42"); // 42
let f = string.to_float("3.14"); // 3.14
let nl = string.from_char(10); // "\n"
let stars = string.repeat("*", 3); // "***"
let padded = string.pad_left("7", 3, "0"); // "007"
}
from_intConvert an integer to its string representation.
let s = string.from_int(42); // "42"
from_floatConvert a float to its string representation.
from_boolConvert a bool to "true" or "false".
from_charConvert a character code to a single-character string.
let newline = string.from_char(10);
to_intParse 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.
Invalid or out-of-range input returns 0.
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
try_to_intParse a string as an integer, returning a structured error on failure.
to_floatParse a string as a float. Returns 0.0 if parsing fails.
The parser accepts:
+ or -e or E)The entire string must be a valid float literal; partial parses fail.
let pi = string.to_float("3.14"); // 3.14
let n = string.to_float("-2e3"); // -2000.0
let z = string.to_float("3.14x"); // 0.0
try_to_floatParse a string as a float, returning a structured error on failure.
is_emptyCheck if a string is empty.
string.is_empty("") // true
string.is_empty("hi") // false
repeatRepeat a string n times.
let stars = string.repeat("*", 5); // "*****"
pad_leftPad a string on the left to reach the given width.
If s is already at least width characters, returns s unchanged.
string.pad_left("42", 5, " ") // " 42"
string.pad_left("42", 5, "0") // "00042"
pad_rightPad a string on the right to reach the given width.
If s is already at least width characters, returns s unchanged.
string.pad_right("hi", 5, " ") // "hi "
is_numericCheck if a string contains only ASCII digits (0-9).
Returns false for empty strings.
string.is_numeric("123") // true
string.is_numeric("12a") // false
string.is_numeric("") // false
countCount the number of non-overlapping occurrences of needle in haystack.
string.count("abcabc", "abc") // 2
string.count("hello", "x") // 0
starts_withCheck if a string starts with the given prefix.
string.starts_with("hello", "he") // true
string.starts_with("hello", "lo") // false
ends_withCheck if a string ends with the given suffix.
string.ends_with("hello", "lo") // true
string.ends_with("hello", "he") // false
containsCheck if a string contains the given substring.
string.contains("hello world", "world") // true
string.contains("hello", "xyz") // false
is_asciiCheck if a string contains only ASCII characters (code points 0–127).
string.is_ascii("hello") // true
string.is_ascii("héllo") // false
to_lowerConvert a string to lowercase.
to_upperConvert a string to uppercase.
trimTrim leading and trailing whitespace from a string.
replaceReplace all occurrences of old in s with new_val.
splitSplit a string by sep into a list of substrings.
An empty separator returns a single-element list containing the original string unchanged (mirrors the FFI behaviour). A trailing delimiter produces a trailing empty element.
import std::string;
let parts = string.split("a,b,c", ","); // ["a", "b", "c"]
let trail = string.split("a,b,", ","); // ["a", "b", ""]
let empty = string.split("x", ""); // ["x"]
linesSplit a string into lines, stripping \r\n or \n endings.
Always emits a final element for the text after the last newline (which will be empty when the string ends with a newline), matching the FFI behaviour.
import std::string;
let lines = string.lines("foo\nbar"); // ["foo", "bar"]
let crlf = string.lines("a\r\nb"); // ["a", "b"]
let trail = string.lines("foo\n"); // ["foo", ""]
joinJoin a list of strings with sep between each element.
Returns an empty string for an empty list.
import std::string;
let s = string.join(["a", "b", "c"], ", "); // "a, b, c"
let e = string.join([], ","); // ""
ToStringTrait for types that can be converted to a string representation.
Provides a standard interface for string conversion across modules.
import std::string;
// Implement for custom types:
// impl ToString for MyType {
// fn to_str(m: MyType) -> string { ... }
// }
Convert this value to its string representation.