Skip to content

string

The string module is auto-imported — no import statement needed.

FunctionSignatureDescription
trim(String) -> StringRemove leading and trailing whitespace.
split(String, String) -> List[String]Split a string by separator into a list of substrings.
join(List[String], String) -> StringJoin a list of strings with a separator.
len(String) -> IntReturn the number of characters in a string.
contains(String, String) -> BoolCheck if a string contains a substring.
starts_with(String, String) -> BoolCheck if a string starts with a prefix.
ends_with(String, String) -> BoolCheck if a string ends with a suffix.
slice(String, Int, Int) -> StringExtract a substring by start and optional end index.
pad_start(String, Int, String) -> StringPad a string on the left to reach a target length.
to_bytes(String) -> List[Int]Convert a string to a list of UTF-8 byte values.
capitalize(String) -> StringCapitalize the first character of a string.
to_upper(String) -> StringConvert all characters to uppercase.
to_lower(String) -> StringConvert all characters to lowercase.
replace(String, String, String) -> StringReplace all occurrences of a substring.
get(String, Int) -> Option[String]Get the character at a given index, or none if out of bounds.
lines(String) -> List[String]Split a string into lines.
chars(String) -> List[String]Split a string into individual characters.
index_of(String, String) -> Option[Int]Find the first index of a substring, or none if not found.
repeat(String, Int) -> StringRepeat a string n times.
from_bytes(List[Int]) -> StringCreate a string from a list of UTF-8 byte values.
is_digit(String) -> BoolCheck if all characters are ASCII digits.
is_alpha(String) -> BoolCheck if all characters are alphabetic.
is_alphanumeric(String) -> BoolCheck if all characters are alphanumeric.
is_whitespace(String) -> BoolCheck if all characters are whitespace.
is_upper(String) -> BoolCheck if all characters in the string are uppercase.
is_lower(String) -> BoolCheck if all characters in the string are lowercase.
codepoint(String) -> Option[Int]Return the Unicode codepoint of the first character, or none for empty string.
from_codepoint(Int) -> StringCreate a single-character string from a Unicode codepoint.
pad_end(String, Int, String) -> StringPad a string on the right to reach a target length.
trim_start(String) -> StringRemove leading whitespace.
trim_end(String) -> StringRemove trailing whitespace.
count(String, String) -> IntCount occurrences of a substring.
is_empty(String) -> BoolCheck if a string is empty.
reverse(String) -> StringReverse the characters in a string.
strip_prefix(String, String) -> Option[String]Remove a prefix if present, returning none if not found.
strip_suffix(String, String) -> Option[String]Remove a suffix if present, returning none if not found.
replace_first(String, String, String) -> StringReplace the first occurrence of a substring.
last_index_of(String, String) -> Option[Int]Find the last index of a substring, or none if not found.
first(String) -> Option[String]Get the first character of a string.
last(String) -> Option[String]Get the last character of a string.
take(String, Int) -> StringTake the first N characters.
take_end(String, Int) -> StringTake the last N characters.
drop(String, Int) -> StringDrop the first N characters.
drop_end(String, Int) -> StringDrop the last N characters.

Remove leading and trailing whitespace.

string.trim(" 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"

Return the number of characters in a string.

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

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

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

Convert a string to a list of UTF-8 byte values.

string.to_bytes("Hi") // => [72, 105]

Capitalize the first character of a string.

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

Convert all characters to uppercase.

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

Convert all characters to lowercase.

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

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.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.char_at("hello", 1) // => some("e")

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

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

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"

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 in the string are uppercase.

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

Check if all characters in the string are lowercase.

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

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"

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

Remove leading whitespace.

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

Remove trailing whitespace.

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

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

Check if a string is empty.

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

Reverse the characters in a string.

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

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

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"