String Methods
Strings in Risor have a rich set of built-in methods for searching, splitting, transforming, and inspecting text. All string methods are non-mutating. They return new strings rather than modifying the original.
Method Reference
| Method | Description |
|---|---|
compare(other) | Compare to another string, returns -1, 0, or 1 |
contains(substr) | True if the string contains substr |
split(sep) | Split into a list on the given separator |
to_upper() | Convert to uppercase |
to_lower() | Convert to lowercase |
trim_space() | Remove leading and trailing whitespace |
trim(chars) | Remove leading and trailing characters |
trim_prefix(prefix) | Remove prefix if present, returns string unchanged if not |
trim_suffix(suffix) | Remove suffix if present, returns string unchanged if not |
has_prefix(prefix) | True if the string starts with prefix |
has_suffix(suffix) | True if the string ends with suffix |
index(substr) | Index of first occurrence, or -1 |
last_index(substr) | Index of last occurrence, or -1 |
replace_all(old, new) | Replace all occurrences of old with new |
join(list) | Join list elements using the string as separator |
count(substr) | Count non-overlapping occurrences of substr |
repeat(n) | Repeat the string n times |
fields() | Split on whitespace into a list of words |
Searching
compare
Compare two strings lexicographically. Returns -1 if the receiver is less than the other string, 0 if they are equal, or 1 if the receiver is greater.
"apple".compare("banana") // -1
"banana".compare("banana") // 0
"cherry".compare("banana") // 1contains
Check whether a string contains a substring.
"hello world".contains("world") // true
"hello world".contains("xyz") // falsehas_prefix
Check whether a string starts with a given prefix.
"hello".has_prefix("he") // true
"hello".has_prefix("lo") // falsehas_suffix
Check whether a string ends with a given suffix.
"hello".has_suffix("lo") // true
"hello".has_suffix("he") // falseindex
Return the index of the first occurrence of a substring, or -1 if not found.
"hello".index("ll") // 2
"hello".index("xyz") // -1last_index
Return the index of the last occurrence of a substring, or -1 if not found.
"hello hello".last_index("hello") // 6
"hello".last_index("xyz") // -1count
Count the number of non-overlapping occurrences of a substring.
"hello hello".count("hello") // 2
"aaa".count("aa") // 1Splitting and Joining
split
Split a string into a list of substrings on a separator.
"hello world".split(" ") // ["hello", "world"]
"a,b,c".split(",") // ["a", "b", "c"]fields
Split a string on whitespace, returning a list of non-empty words. This handles multiple spaces, tabs, and newlines.
"hello world".fields() // ["hello", "world"]
" foo bar baz ".fields() // ["foo", "bar", "baz"]join
Join a list of strings using the receiver as the separator.
", ".join(["a", "b", "c"]) // "a, b, c"
"-".join(["2024", "01", "15"]) // "2024-01-15"Note that the separator is the receiver and the list is the argument.
Transforming
to_upper
Convert a string to uppercase.
"hello".to_upper() // "HELLO"to_lower
Convert a string to lowercase.
"HELLO".to_lower() // "hello"replace_all
Replace all occurrences of a substring with another string.
"hello".replace_all("l", "r") // "herro"
"foo bar foo".replace_all("foo", "baz") // "baz bar baz"repeat
Repeat a string a given number of times.
"abc".repeat(3) // "abcabcabc"
"ha".repeat(0) // ""Trimming
trim_space
Remove leading and trailing whitespace (spaces, tabs, newlines).
" hello ".trim_space() // "hello"
"\thello\n".trim_space() // "hello"trim
Remove leading and trailing characters from a set.
" hello ".trim(" ") // "hello"
"xxhelloxx".trim("x") // "hello"trim_prefix
Remove a prefix from the string if present. If the string does not start with the prefix, it is returned unchanged.
"hello world".trim_prefix("hello ") // "world"
"hello world".trim_prefix("xyz") // "hello world"trim_suffix
Remove a suffix from the string if present. If the string does not end with the suffix, it is returned unchanged.
"hello world".trim_suffix(" world") // "hello"
"hello world".trim_suffix("xyz") // "hello world"