Skip to content

std/string

module string for String

The functions of a String, in the prelude. A receiver module: each function takes self, so s.trim(), string.trim(s) and s | trim are one declaration. A String is a sequence of Unicode scalar values held as UTF-8, whatever encoding its text was read from. Positions and size count code points.

  • str — The text self prints as, for a value of any type: str(some(2)) is "2".
  • fmt — The template self with each {} replaced by the text of the next of args, in order: fmt("{} of {}", 1, some(2)) is "1 of 2".
  • length — How many characters self has, counting code points: "héllo" | length is 5.
  • size — How many characters self has, counting code points; the same as length: "héllo" | size is 5.
  • upper — self in upper case: "Straße" | upper is "STRASSE".
  • lower — self in lower case: "ÀB" | lower is "àb".
  • trim — self without leading and trailing whitespace: " a b " | trim is "a b".
  • trim_start — self without leading whitespace: " a " | trim_start is "a ".
  • trim_end — self without trailing whitespace: " a " | trim_end is " a".
  • capitalize — self with its first character in upper case: "élan" | capitalize is "Élan".
  • split — The pieces of self between occurrences of separator, in order: "a,b,,c" | split(",") is ["a", "b", "", "c"].
  • join — The texts of self in order, with separator between each two: ["a", "b"].join(", ") is "a, b".
  • replace_all — self with every occurrence of from replaced by to: "aaa" | replace_all("a", "b") is "bbb".
  • replace_first — self with the first occurrence of from replaced by to: "aaa" | replace_first("a", "b") is "baa".
  • starts_with — Whether self begins with part: "hello" | starts_with("he") is true.
  • ends_with — Whether self ends with part: "hello" | ends_with("lo") is true.
  • contains — Whether part occurs in self: "hello" | contains("ell") is true.
  • index_of — The position of the first occurrence of part in self, counting characters from 0, or none: "héllo" | index_of("l") is 2.
  • last_index_of — The position of the last occurrence of part in self, counting characters from 0, or none: "héllo" | last_index_of("l") is 3.
  • occurrences — How many non-overlapping times part occurs in self; 0 for an empty part: "aaaa" | occurrences("aa") is 2.
  • strip_prefix — self without part at its start, or none when it does not begin with it: "x.hql" | strip_prefix("x.") is "hql".
  • strip_suffix — self without part at its end, or none when it does not end with it: "x.hql" | strip_suffix(".hql") is "x".
  • trim_prefix — self without part at its start, or self as it is when it does not begin with it: "x" | trim_prefix("y") is "x".
  • trim_suffix — self without part at its end, or self as it is when it does not end with it: "x.hql" | trim_suffix(".hql") is "x".
  • equals_ignore_case — Whether self and other are equal in lower case: "ABC" | equals_ignore_case("abc") is true.
  • chars — Each character of self as its own text, in order: "héy" | chars is ["h", "é", "y"].
  • lines — The lines of self, without their line breaks; a final line break opens no empty line: string.lines("a\nb\n") is ["a", "b"].
  • words — The whitespace-separated words of self: " a b\tc " | words is ["a", "b", "c"].
  • reverse — The characters of self in the opposite order: "héllo" | reverse is "olléh".
  • repeat — self written times times in a row; "" for times below 1: "ab" | repeat(3) is "ababab".
  • pad_left — self preceded by fill, repeated, until it is width characters long; a longer self as it is: "7" | pad_left(3, "0") is "007".
  • pad_right — self followed by fill, repeated, until it is width characters long; a longer self as it is: "7" | pad_right(3, "0") is "700".
  • center — self between fills until it is width characters long; an odd fill character goes after it: "ab" | center(5, "*") is "*ab**".
  • truncate — At most the first width characters of self: "hello" | truncate(3) is "hel".
  • slice — The characters of self from start up to, not including, end, counting from 0; s[start:end].
  • char_at — The character of self at position, counting from 0, or none outside the text: "hello" | char_at(1) is "e".
  • to_int — The integer self writes, surrounding whitespace aside, or none when it writes none: "-42" | to_int is -42, and "1.5" | to_int is none.
  • to_float — The number self writes, as a Float, or none when it writes none: "1e3" | to_float is 1000.0.
  • to_decimal — The exact decimal number self writes, at its written scale, or none when it writes none: "2.50" | to_decimal is 2.50.
  • to_bool — The truth value self writes as true or false, or none for any other text: "True" | to_bool is none.
  • is_empty — Whether self has no characters: "" | is_empty is true.
  • is_blank — Whether self has no characters other than whitespace: " " | is_blank is true.
  • ord — The Unicode code point of the one-character text self, or none for any other text: "a" | ord is 97.
  • chr — The one-character text of the Unicode code point self, or none for an Int that is none: 97 | chr is "a".
  • bytes — The UTF-8 encoding of self, one Int per byte: "é" | bytes is [195, 169].