string
The string module provides functions for string manipulation. It is auto-imported — no import statement needed.
Core Operations
Section titled “Core Operations”string.len(s: String) -> Int
Section titled “string.len(s: String) -> Int”Return the number of characters in a string.
string.len("hello") // => 5string.len("") // => 0string.is_empty(s: String) -> Bool
Section titled “string.is_empty(s: String) -> Bool”Check if a string is empty.
string.is_empty("") // => truestring.is_empty("a") // => falsestring.contains(s: String, sub: String) -> Bool
Section titled “string.contains(s: String, sub: String) -> Bool”Check if a string contains a substring.
string.contains("hello world", "world") // => truestring.reverse(s: String) -> String
Section titled “string.reverse(s: String) -> String”Reverse the characters in a string.
string.reverse("hello") // => "olleh"Trimming
Section titled “Trimming”string.trim(s: String) -> String
Section titled “string.trim(s: String) -> String”Remove leading and trailing whitespace.
string.trim(" hello ") // => "hello"string.trim_start(s: String) -> String
Section titled “string.trim_start(s: String) -> String”Remove leading whitespace.
string.trim_start(" hello") // => "hello"string.trim_end(s: String) -> String
Section titled “string.trim_end(s: String) -> String”Remove trailing whitespace.
string.trim_end("hello ") // => "hello"Splitting and Joining
Section titled “Splitting and Joining”string.split(s: String, sep: String) -> List[String]
Section titled “string.split(s: String, sep: String) -> List[String]”Split a string by separator into a list of substrings.
string.split("a,b,c", ",") // => ["a", "b", "c"]string.join(list: List[String], sep: String) -> String
Section titled “string.join(list: List[String], sep: String) -> String”Join a list of strings with a separator.
string.join(["a", "b", "c"], "-") // => "a-b-c"string.lines(s: String) -> List[String]
Section titled “string.lines(s: String) -> List[String]”Split a string into lines.
string.lines("a\nb\nc") // => ["a", "b", "c"]string.chars(s: String) -> List[String]
Section titled “string.chars(s: String) -> List[String]”Split a string into individual characters.
string.chars("abc") // => ["a", "b", "c"]Case Conversion
Section titled “Case Conversion”string.to_upper(s: String) -> String
Section titled “string.to_upper(s: String) -> String”Convert all characters to uppercase.
string.to_upper("hello") // => "HELLO"string.to_lower(s: String) -> String
Section titled “string.to_lower(s: String) -> String”Convert all characters to lowercase.
string.to_lower("HELLO") // => "hello"string.capitalize(s: String) -> String
Section titled “string.capitalize(s: String) -> String”Capitalize the first character of a string.
string.capitalize("hello") // => "Hello"Search
Section titled “Search”string.starts_with(s: String, prefix: String) -> Bool
Section titled “string.starts_with(s: String, prefix: String) -> Bool”Check if a string starts with a prefix.
string.starts_with("hello", "hel") // => truestring.ends_with(s: String, suffix: String) -> Bool
Section titled “string.ends_with(s: String, suffix: String) -> Bool”Check if a string ends with a suffix.
string.ends_with("hello", "llo") // => truestring.index_of(s: String, needle: String) -> Option[Int]
Section titled “string.index_of(s: String, needle: String) -> Option[Int]”Find the first index of a substring, or none if not found.
string.index_of("hello", "ll") // => some(2)string.last_index_of(s: String, needle: String) -> Option[Int]
Section titled “string.last_index_of(s: String, needle: String) -> Option[Int]”Find the last index of a substring, or none if not found.
string.last_index_of("abcabc", "bc") // => some(4)string.count(s: String, sub: String) -> Int
Section titled “string.count(s: String, sub: String) -> Int”Count occurrences of a substring.
string.count("banana", "an") // => 2Replacement
Section titled “Replacement”string.replace(s: String, from: String, to: String) -> String
Section titled “string.replace(s: String, from: String, to: String) -> String”Replace all occurrences of a substring.
string.replace("aabbcc", "bb", "XX") // => "aaXXcc"string.replace_first(s: String, from: String, to: String) -> String
Section titled “string.replace_first(s: String, from: String, to: String) -> String”Replace the first occurrence of a substring.
string.replace_first("aabaa", "a", "X") // => "Xabaa"Slicing and Substrings
Section titled “Slicing and Substrings”string.slice(s: String, start: Int, end?: Int) -> String
Section titled “string.slice(s: String, start: Int, end?: Int) -> String”Extract a substring by start and optional end index.
string.slice("hello", 1, 4) // => "ell"string.slice("hello", 2) // => "llo"string.get(s: String, i: Int) -> Option[String]
Section titled “string.get(s: String, i: Int) -> Option[String]”Get the character at a given index, or none if out of bounds.
string.get("hello", 1) // => some("e")string.first(s: String) -> Option[String]
Section titled “string.first(s: String) -> Option[String]”Get the first character of a string.
string.first("hello") // => some("h")string.last(s: String) -> Option[String]
Section titled “string.last(s: String) -> Option[String]”Get the last character of a string.
string.last("hello") // => some("o")string.take(s: String, n: Int) -> String
Section titled “string.take(s: String, n: Int) -> String”Take the first N characters.
string.take("hello", 3) // => "hel"string.take_end(s: String, n: Int) -> String
Section titled “string.take_end(s: String, n: Int) -> String”Take the last N characters.
string.take_end("hello", 3) // => "llo"string.drop(s: String, n: Int) -> String
Section titled “string.drop(s: String, n: Int) -> String”Drop the first N characters.
string.drop("hello", 2) // => "llo"string.drop_end(s: String, n: Int) -> String
Section titled “string.drop_end(s: String, n: Int) -> String”Drop the last N characters.
string.drop_end("hello", 2) // => "hel"Prefix and Suffix
Section titled “Prefix and Suffix”string.strip_prefix(s: String, prefix: String) -> Option[String]
Section titled “string.strip_prefix(s: String, prefix: String) -> Option[String]”Remove a prefix if present, returning none if not found.
string.strip_prefix("hello", "hel") // => some("lo")string.strip_suffix(s: String, suffix: String) -> Option[String]
Section titled “string.strip_suffix(s: String, suffix: String) -> Option[String]”Remove a suffix if present, returning none if not found.
string.strip_suffix("hello", "llo") // => some("he")Padding and Repetition
Section titled “Padding and Repetition”string.pad_start(s: String, n: Int, ch: String) -> String
Section titled “string.pad_start(s: String, n: Int, ch: String) -> String”Pad a string on the left to reach a target length.
string.pad_start("42", 5, "0") // => "00042"string.pad_end(s: String, n: Int, ch: String) -> String
Section titled “string.pad_end(s: String, n: Int, ch: String) -> String”Pad a string on the right to reach a target length.
string.pad_end("hi", 5, ".") // => "hi..."string.repeat(s: String, n: Int) -> String
Section titled “string.repeat(s: String, n: Int) -> String”Repeat a string n times.
string.repeat("ab", 3) // => "ababab"Character Classification
Section titled “Character Classification”string.is_digit(s: String) -> Bool
Section titled “string.is_digit(s: String) -> Bool”Check if all characters are ASCII digits.
string.is_digit("123") // => truestring.is_alpha(s: String) -> Bool
Section titled “string.is_alpha(s: String) -> Bool”Check if all characters are alphabetic.
string.is_alpha("abc") // => truestring.is_alphanumeric(s: String) -> Bool
Section titled “string.is_alphanumeric(s: String) -> Bool”Check if all characters are alphanumeric.
string.is_alphanumeric("abc123") // => truestring.is_whitespace(s: String) -> Bool
Section titled “string.is_whitespace(s: String) -> Bool”Check if all characters are whitespace.
string.is_whitespace(" ") // => truestring.is_upper(s: String) -> Bool
Section titled “string.is_upper(s: String) -> Bool”Check if all characters are uppercase.
string.is_upper("ABC") // => truestring.is_lower(s: String) -> Bool
Section titled “string.is_lower(s: String) -> Bool”Check if all characters are lowercase.
string.is_lower("abc") // => trueByte Conversion
Section titled “Byte Conversion”string.to_bytes(s: String) -> List[Int]
Section titled “string.to_bytes(s: String) -> List[Int]”Convert a string to a list of UTF-8 byte values.
string.to_bytes("Hi") // => [72, 105]string.from_bytes(bytes: List[Int]) -> String
Section titled “string.from_bytes(bytes: List[Int]) -> String”Create a string from a list of UTF-8 byte values.
string.from_bytes([72, 105]) // => "Hi"Unicode
Section titled “Unicode”string.codepoint(s: String) -> Option[Int]
Section titled “string.codepoint(s: String) -> Option[Int]”Return the Unicode codepoint of the first character, or none for empty string.
string.codepoint("A") // => some(65)string.from_codepoint(n: Int) -> String
Section titled “string.from_codepoint(n: Int) -> String”Create a single-character string from a Unicode codepoint.
string.from_codepoint(65) // => "A"Complete Function Reference
Section titled “Complete Function Reference”| Function | Signature | Description |
|---|---|---|
len | (String) -> Int | Character count |
is_empty | (String) -> Bool | Empty check |
trim | (String) -> String | Strip whitespace |
trim_start | (String) -> String | Strip leading whitespace |
trim_end | (String) -> String | Strip trailing whitespace |
split | (String, String) -> List[String] | Split by separator |
join | (List[String], String) -> String | Join with separator |
lines | (String) -> List[String] | Split into lines |
chars | (String) -> List[String] | Split into characters |
contains | (String, String) -> Bool | Substring check |
starts_with | (String, String) -> Bool | Prefix check |
ends_with | (String, String) -> Bool | Suffix check |
index_of | (String, String) -> Option[Int] | First occurrence |
last_index_of | (String, String) -> Option[Int] | Last occurrence |
count | (String, String) -> Int | Count occurrences |
replace | (String, String, String) -> String | Replace all |
replace_first | (String, String, String) -> String | Replace first |
slice | (String, Int, Int?) -> String | Substring extraction |
get | (String, Int) -> Option[String] | Character at index |
first | (String) -> Option[String] | First character |
last | (String) -> Option[String] | Last character |
take | (String, Int) -> String | First N characters |
take_end | (String, Int) -> String | Last N characters |
drop | (String, Int) -> String | Drop first N |
drop_end | (String, Int) -> String | Drop last N |
strip_prefix | (String, String) -> Option[String] | Remove prefix |
strip_suffix | (String, String) -> Option[String] | Remove suffix |
to_upper | (String) -> String | Uppercase |
to_lower | (String) -> String | Lowercase |
capitalize | (String) -> String | Capitalize first |
pad_start | (String, Int, String) -> String | Left pad |
pad_end | (String, Int, String) -> String | Right pad |
repeat | (String, Int) -> String | Repeat N times |
reverse | (String) -> String | Reverse characters |
to_bytes | (String) -> List[Int] | UTF-8 bytes |
from_bytes | (List[Int]) -> String | From UTF-8 bytes |
codepoint | (String) -> Option[Int] | Unicode codepoint |
from_codepoint | (Int) -> String | From codepoint |
is_digit | (String) -> Bool | ASCII digit check |
is_alpha | (String) -> Bool | Alphabetic check |
is_alphanumeric | (String) -> Bool | Alphanumeric check |
is_whitespace | (String) -> Bool | Whitespace check |
is_upper | (String) -> Bool | Uppercase check |
is_lower | (String) -> Bool | Lowercase check |