Standard Library
std.string
String manipulation utilities for common text operations.
Exported functions
Find the first occurrence of a substring.
Return true when the string contains a substring.
Split a string by a delimiter.
Examples
use string from "std.string"
string.split("red,green,blue", ",") # => ["red", "green", "blue"]Find the last occurrence of a substring.
Check if a string starts with a given prefix.
Examples
use string from "std.string"
string.starts_with("hello world", "hello") # => true
string.starts_with("hello world", "world") # => falseCheck if a string ends with a given suffix.
Examples
string.ends_with("hello world", "world") # => true
string.ends_with("hello world", "hello") # => falseTrim whitespace from the start of a string.
Examples
string.trim_start(" hello ") # => "hello "
string.trim_start("\t\nhello") # => "hello"Trim whitespace from the end of a string.
Examples
string.trim_end(" hello ") # => " hello"
string.trim_end("hello\n\t") # => "hello"Trim whitespace from both ends of a string.
Examples
string.trim(" hello ") # => "hello"
string.trim("\t\nhello\n\t") # => "hello"Reverse a string.
Examples
string.reverse("hello") # => "olleh"
string.reverse("ab") # => "ba"Replace all occurrences of a substring with another string.
Examples
string.replace("hello world", "world", "tea") # => "hello tea"
string.replace("foo bar foo", "foo", "baz") # => "baz bar baz"Replace the first occurrence of a substring with another string.
Convert a string to lowercase.
Examples
string.to_lower("Hello World") # => "hello world"
string.to_lower("TEA") # => "tea"Convert a string to uppercase.
Examples
string.to_upper("Hello World") # => "HELLO WORLD"
string.to_upper("tea") # => "TEA"Join a list of strings with a separator.
Split text into lines, trimming a trailing "\r" from each line and dropping the final empty line produced by a trailing newline.
Split a string into two parts at the first occurrence of a delimiter.
Examples
use string from "std.string"
const parts = string.split_once("name=green=tea", "=")
if parts != nil
const pair = parts!
@println(pair[0]) # name
@println(pair[1]) # green=tea
end
@println(string.split_once("name", "=") == nil) # trueSplit a string into two parts at the last occurrence of a delimiter.
Examples
use string from "std.string"
const parts = string.rsplit_once("archive.tar.gz", ".")
if parts != nil
@println(parts!) # [archive.tar, gz]
endCount non-overlapping occurrences of a substring.
Remove a prefix when present.
Examples
use string from "std.string"
@println(string.strip_prefix("Bearer token123", "Bearer ") ?? "") # token123
@println(string.strip_prefix("token123", "Bearer ") == nil) # trueRemove a suffix when present.
Examples
use string from "std.string"
@println(string.strip_suffix("report.txt", ".txt") ?? "report.txt") # reportRepeat a string count times.
Pad a string on the left with a fill string until it reaches width.
Examples
use string from "std.string"
@println(string.pad_start_with("42", 5, "0")) # 00042Pad a string on the left with spaces until it reaches width.
Pad a string on the right with a fill string until it reaches width.
Pad a string on the right with spaces until it reaches width.
Examples
use string from "std.string"
@println(string.pad_end("tea", 6) + "|") # tea |