Skip to content

string

The string module provides functions for string manipulation. It is auto-imported — no import statement needed.

Return the number of characters in a string.

string.len("hello") // => 5
string.len("") // => 0

Check if a string is empty.

string.is_empty("") // => true
string.is_empty("a") // => false

string.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") // => true

Reverse the characters in a string.

string.reverse("hello") // => "olleh"

Remove leading and trailing whitespace.

string.trim(" hello ") // => "hello"

Remove leading whitespace.

string.trim_start(" hello") // => "hello"

Remove trailing whitespace.

string.trim_end("hello ") // => "hello"

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"

Split a string into lines.

string.lines("a\nb\nc") // => ["a", "b", "c"]

Split a string into individual characters.

string.chars("abc") // => ["a", "b", "c"]

Convert all characters to uppercase.

string.to_upper("hello") // => "HELLO"

Convert all characters to lowercase.

string.to_lower("HELLO") // => "hello"

Capitalize the first character of a string.

string.capitalize("hello") // => "Hello"

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") // => true

string.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") // => true

string.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") // => 2

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"

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")

Get the first character of a string.

string.first("hello") // => some("h")

Get the last character of a string.

string.last("hello") // => some("o")

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"

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"

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")

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"

Check if all characters are ASCII digits.

string.is_digit("123") // => true

Check if all characters are alphabetic.

string.is_alpha("abc") // => true

Check if all characters are alphanumeric.

string.is_alphanumeric("abc123") // => true

Check if all characters are whitespace.

string.is_whitespace(" ") // => true

Check if all characters are uppercase.

string.is_upper("ABC") // => true

Check if all characters are lowercase.

string.is_lower("abc") // => true

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"

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)

Create a single-character string from a Unicode codepoint.

string.from_codepoint(65) // => "A"
FunctionSignatureDescription
len(String) -> IntCharacter count
is_empty(String) -> BoolEmpty check
trim(String) -> StringStrip whitespace
trim_start(String) -> StringStrip leading whitespace
trim_end(String) -> StringStrip trailing whitespace
split(String, String) -> List[String]Split by separator
join(List[String], String) -> StringJoin with separator
lines(String) -> List[String]Split into lines
chars(String) -> List[String]Split into characters
contains(String, String) -> BoolSubstring check
starts_with(String, String) -> BoolPrefix check
ends_with(String, String) -> BoolSuffix check
index_of(String, String) -> Option[Int]First occurrence
last_index_of(String, String) -> Option[Int]Last occurrence
count(String, String) -> IntCount occurrences
replace(String, String, String) -> StringReplace all
replace_first(String, String, String) -> StringReplace first
slice(String, Int, Int?) -> StringSubstring extraction
get(String, Int) -> Option[String]Character at index
first(String) -> Option[String]First character
last(String) -> Option[String]Last character
take(String, Int) -> StringFirst N characters
take_end(String, Int) -> StringLast N characters
drop(String, Int) -> StringDrop first N
drop_end(String, Int) -> StringDrop last N
strip_prefix(String, String) -> Option[String]Remove prefix
strip_suffix(String, String) -> Option[String]Remove suffix
to_upper(String) -> StringUppercase
to_lower(String) -> StringLowercase
capitalize(String) -> StringCapitalize first
pad_start(String, Int, String) -> StringLeft pad
pad_end(String, Int, String) -> StringRight pad
repeat(String, Int) -> StringRepeat N times
reverse(String) -> StringReverse characters
to_bytes(String) -> List[Int]UTF-8 bytes
from_bytes(List[Int]) -> StringFrom UTF-8 bytes
codepoint(String) -> Option[Int]Unicode codepoint
from_codepoint(Int) -> StringFrom codepoint
is_digit(String) -> BoolASCII digit check
is_alpha(String) -> BoolAlphabetic check
is_alphanumeric(String) -> BoolAlphanumeric check
is_whitespace(String) -> BoolWhitespace check
is_upper(String) -> BoolUppercase check
is_lower(String) -> BoolLowercase check