std.string
Converts values to text and joins strings; the compiler calls these functions by name.
f-strings and + on strings lower to concat and the from… conversions. The module also
re-exports StringBuilder for building text piece by piece, and names the Unicode version
behind char in unicodeVersion.
Examples
import std.io { println };
import std.string { StringBuilder };
let sb = StringBuilder.new();
for (i in 1..=3) {
sb.append(f"{i},");
}
println(sb.toString());
1,2,3,
Types
Functions
pub extern "C" fn concat(a: string, b: string): string = "lyr_str_concat"
Returns a followed by b.
pub extern "C" fn fromInt(value: int): string = "lyr_str_from_int"
Returns the decimal text of a signed integer.
pub extern "C" fn fromUint(value: uint): string = "lyr_str_from_uint"
Returns the decimal text of an unsigned integer.
pub extern "C" fn fromFloat(value: float): string = "lyr_str_from_float"
Returns the shortest decimal text that reads back as the same float.
Infinities are written inf and -inf, a NaN as NaN.
pub extern "C" fn fromFloat32(value: float32): string = "lyr_str_from_float32"
Returns the shortest decimal text that reads back as the same float32.
The text is the float32's own: 0.1, not the 0.10000000149011612 of the float it
widens to.
pub extern "C" fn fromChar(value: char): string = "lyr_str_from_char"
Returns the character as a one-character string.
pub extern "C" fn fromBool(value: bool): string = "lyr_str_from_bool"
Returns true or false.
Constants
pub let unicodeVersion: string = "18.0.0"
The version of the Unicode Character Database behind char's answers.
It governs a character's general category, its simple case mappings and isWhitespace.
Extensions
pub extend StringView
pub fn contains<P :: [Pattern]>(p: P): bool
Returns whether p stands in it.
pub fn startsWith<P :: [Pattern]>(p: P): bool
Returns whether it begins with p.
pub fn endsWith<P :: [Pattern]>(p: P): bool
Returns whether it ends with p.
pub fn find<P :: [Pattern]>(p: P): ?int
Returns the byte index of the first p, or null; an empty p stands at 0.
pub fn rfind<P :: [Pattern]>(p: P): ?int
Returns the byte index of the last p, or null; an empty p stands at the length.
pub fn count<P :: [Pattern]>(p: P): int
Returns how many times p stands in it, none overlapping another.
An empty p stands between every two characters and at both ends.
pub fn split<P :: [Pattern]>(sep: P): SplitIter<P>
Returns an iterator over the parts between the separators, in order.
"a,b,,c".split(",") gives a, b, ``, c, views with no copies.
Panics
When sep matches the empty text.
pub fn splitN<P :: [Pattern]>(sep: P, n: int): SplitIter<P>
Returns an iterator over at most n parts between the separators.
The first n - 1 separators split, the rest is the last part. n of 0 gives none.
Panics
When sep matches the empty text, or n is negative.
pub fn splitOnce<P :: [Pattern]>(sep: P): ?(StringView, StringView)
Returns the part before the first sep and the part after it, or null where there is
none.
pub fn findAll<P :: [Pattern]>(p: P): MatchesIter<P>
Returns an iterator over the matches of p as views, in order, none overlapping another.
pub fn lines(): LinesIter
Returns an iterator over the lines.
Split at \n, a \r before it taken off, no empty line after a last \n.
pub fn trim(): StringView
Returns the text without the white space at both ends.
White space is the characters with the property White_Space: the ideographic space and
the no-break space as well as the tab. trimMatches trims a pattern of one's own.
pub fn trimStart(): StringView
Returns the text without the white space at the start.
pub fn trimEnd(): StringView
Returns the text without the white space at the end.
pub fn trimMatches<P :: [Pattern]>(p: P): StringView
Returns the text without the matches of p at both ends, removed again and again.
"xxaxx".trimMatches('x') is a. A match of the empty text takes nothing.
pub fn trimStartMatches<P :: [Pattern]>(p: P): StringView
Returns the text without the matches of p at the start, removed again and again.
pub fn trimEndMatches<P :: [Pattern]>(p: P): StringView
Returns the text without the matches of p at the end, removed again and again.
pub fn isBlank(): bool
Returns whether it holds nothing but white space; true for the empty text too.
pub fn toUpper(): string
Returns the text with every character by its simple uppercase mapping.
One character for one and no locale, so "straße".toUpper() is STRAßE.
pub fn toLower(): string
Returns the text with every character by its simple lowercase mapping.
One character for one, no locale.
pub fn parse<T :: [Parse]>(): T throws ParseError
Parses the text as T.parse(this), the type taken from the expected type.
let n: int = try s.parse();.
Errors
The ParseError T.parse throws.
pub fn stripPrefix<P :: [Pattern]>(p: P): ?StringView
Returns the rest after p where it begins with p, or null.
pub fn stripSuffix<P :: [Pattern]>(p: P): ?StringView
Returns the rest before p where it ends with p, or null.
pub fn replace<P :: [Pattern]>(p: P, with: StringView): string
Returns the text with every p replaced by with, none overlapping another.
Panics
When p matches the empty text.
pub fn replaceN<P :: [Pattern]>(p: P, with: StringView, n: int): string
Returns the text with the first n of p replaced by with; all of them where n is
negative.
Panics
When p matches the empty text.
pub fn padStart(width: int, fill: char = ' '): string
Returns the text with fill before it until it is width characters long; a longer one
as it is.
pub fn padEnd(width: int, fill: char = ' '): string
Returns the text with fill after it until it is width characters long; a longer one
as it is.
pub extend char
pub fn toUpper(): char
Returns the uppercase character by the simple mapping; the character itself where it has none.
pub fn toLower(): char
Returns the lowercase character by the simple mapping; the character itself where it has none.
pub fn toDigit(radix: int): ?int
Returns the character's value as a digit in base radix, or null where it is no digit
of the base.
The digits are 0–9, then a–z or A–Z for 10–35; ASCII alone, as parse reads.
Panics
When radix is outside 2–36.
pub extend<T :: [Display]> Slice<T>
pub fn join(sep: string): string
Joins the elements' texts, sep between each two.
pub extend StringView
pub fn substring(start: int, end: int): StringView
Returns the view of the characters from start up to but not including end, counted in
characters: "héllo".substring(1, 3) is "él".
It walks the bytes up to end, O(end); s[a..b] cuts by bytes in O(1).
Panics
When 0 ≤ start ≤ end ≤ charCount() does not hold.
pub fn charAt(i: int): char
Returns character i, counted in characters: "héllo".charAt(1) is 'é'.
It walks the bytes up to i, O(i); s[i] takes the character at byte i in O(1).
Panics
When 0 ≤ i < charCount() does not hold.
pub fn charIndex(byte: int): int
Returns the number of characters before byte byte: "héllo".charIndex(3) is 2.
Panics
When byte is no character's first byte and not the length.
pub fn isCharBoundary(byte: int): bool
Returns whether a character begins at byte byte, or byte is the length — where a text
can be cut. Outside 0…length() it is false.
pub fn findFrom<P :: [Pattern]>(p: P, from: int): ?int
Returns the byte index of the first p at or after byte from, or null.
Panics
When from is no character's first byte and not the length.
pub fn firstOf(chars: StringView): ?int
Returns the byte index of the first character that is one of chars, or null.
pub fn splitWhitespace(): SplitWhitespaceIter
Returns an iterator over the runs of characters between white space, as views; no empty one — leading, trailing and repeated white space separates nothing.
pub fn rsplitOnce<P :: [Pattern]>(sep: P): ?(StringView, StringView)
Returns the part before the last sep and the part after it, or null where there is
none.
pub fn repeat(n: int): string
Returns the text n times over; "ab".repeat(3) is "ababab".
Panics
When n is negative.
pub fn isAscii(): bool
Returns whether every character is ASCII, below U+0080; the empty text is.
pub fn equalsIgnoreCase(o: StringView): bool
Returns whether the two texts are equal when each character is taken by its simple
lowercase mapping: "Straße".equalsIgnoreCase("STRASSE") is false, as ß maps to itself.
pub fn compareIgnoreCase(o: StringView): Ordering
Compares the two texts character by character, each taken by its simple lowercase mapping; a text that ends first comes first.