Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Strings

ItemSummary
lenReturns the number of characters
containsReturns whether needle appears anywhere in the string
starts_withReturns whether the string begins with prefix
ends_withReturns whether the string ends with suffix
to_upperReturns the string with every letter in uppercase
to_lowerReturns the string with every letter in lowercase, following Unicode’s rules
trimReturns the string without the whitespace at its start and end
to_snakeReturns the string in snake case: lowercase words joined by underscores
splitReturns the pieces of the string between each occurrence of separator
findSearches the string for the first match of the regular expression pattern
find_allReturns every match of the regular expression pattern, each in the form find returns
linesReturns the string’s lines, without their line endings
capitalizeReturns the string with its first character in uppercase
to_intParses the string as a base-10 int, or returns null if it isn’t one
ordReturns the Unicode code point of the first character, or null if the string is empty
is_emptyReturns whether the string has no characters
replaceReturns the string with every occurrence of needle replaced by replacement
repeatReturns the string repeated count times

Methods

len

fn len(self) -> int

Returns the number of characters. This counts Unicode characters, not bytes: "héllo".len() is 5.

let n = "hello".len(); // 5

contains

fn contains(self, needle: str) -> bool

Returns whether needle appears anywhere in the string. This is the same check as the in operator.

let a = "hello".contains("ell"); // true
let b = "ell" in "hello";        // true

starts_with

fn starts_with(self, prefix: str) -> bool

Returns whether the string begins with prefix.

let a = "mimas".starts_with("mi"); // true

ends_with

fn ends_with(self, suffix: str) -> bool

Returns whether the string ends with suffix.

let a = "readme.md".ends_with(".md"); // true

to_upper

fn to_upper(self) -> str

Returns the string with every letter in uppercase. This follows Unicode’s rules, which can turn one character into several.

let a = "Hello".to_upper();  // "HELLO"
let b = "straße".to_upper(); // "STRASSE"

to_lower

fn to_lower(self) -> str

Returns the string with every letter in lowercase, following Unicode’s rules.

let a = "Hello".to_lower(); // "hello"

trim

fn trim(self) -> str

Returns the string without the whitespace at its start and end. Whitespace inside the string stays.

let a = "  two words \n".trim(); // "two words"

to_snake

fn to_snake(self) -> str

Returns the string in snake case: lowercase words joined by underscores. Spaces, punctuation, and changes from lowercase to uppercase all mark where one word ends and the next begins.

let a = "PlayerHealth".to_snake(); // "player_health"
let b = "max hp".to_snake();       // "max_hp"
let c = "HTTPServer".to_snake();   // "http_server"

split

fn split(self, separator: str) -> [str]

Returns the pieces of the string between each occurrence of separator. Two separators in a row leave an empty string between them.

let a = "a,b,c".split(",");     // ["a", "b", "c"]
let b = "a,,b".split(",");      // ["a", "", "b"]
let c = "no commas".split(","); // ["no commas"]

find

fn find(self, pattern: str) -> [str]?

Searches the string for the first match of the regular expression pattern. Returns the whole match followed by the text of each capture group, or null if nothing matched or pattern isn’t a valid regular expression.

A capture group that didn’t take part in the match is left out of the array, which moves the groups after it down a position.

The pattern syntax is that of the Rust regex crate. Inside a mimas string, each backslash in the pattern is written twice ("\\d+" matches a run of digits).

let date = "2024-06-01".find("(\\d+)-(\\d+)-(\\d+)");
// date is ["2024-06-01", "2024", "06", "01"]
let none = "abc".find("\\d"); // null

find_all

fn find_all(self, pattern: str) -> [[str]]?

Returns every match of the regular expression pattern, each in the form find returns. Matches don’t overlap. The array is empty when nothing matched, and the result is null only when pattern isn’t a valid regular expression.

let pairs = "x=1, y=22".find_all("(\\w)=(\\d+)");
// pairs is [["x=1", "x", "1"], ["y=22", "y", "22"]]

lines

fn lines(self) -> [str]

Returns the string’s lines, without their line endings. A line can end in \n or \r\n, and a line ending at the very end of the string doesn’t add an empty line.

let a = "one\ntwo\r\nthree\n".lines(); // ["one", "two", "three"]

capitalize

fn capitalize(self) -> str

Returns the string with its first character in uppercase. The rest of the string is unchanged.

let a = "élan".capitalize();  // "Élan"
let b = "hELLO".capitalize(); // "HELLO"

to_int

fn to_int(self) -> int?

Parses the string as a base-10 int, or returns null if it isn’t one. Whitespace at either end is ignored, and a leading + or - is allowed.

let a = " -12 ".to_int(); // -12
let b = "12px".to_int();  // null
let c = "4.2".to_int();   // null

ord

fn ord(self) -> int?

Returns the Unicode code point of the first character, or null if the string is empty.

let a = "A".ord(); // 65
let b = "".ord();  // null

is_empty

fn is_empty(self) -> bool

Returns whether the string has no characters. A string of spaces isn’t empty.

let a = "".is_empty();  // true
let b = " ".is_empty(); // false

replace

fn replace(self, needle: str, replacement: str) -> str

Returns the string with every occurrence of needle replaced by replacement.

let a = "a-b-c".replace("-", "+"); // "a+b+c"

repeat

fn repeat(self, count: int) -> str

Returns the string repeated count times. A count of 0 gives an empty string.

A negative count is a runtime error.

let a = "ab".repeat(3);   // "ababab"
let line = "-".repeat(20);