std/strings
Functions
fn EqualFold(mut s: str, mut t: str): bool
Reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
fn Compare(a: str, b: str): int
Returns an integer comparing two strings lexicographically. The result will be 0
if a == b
, -1
if a < b
, and +1
if a > b
.
Use compare when you need to perform a three-way comparison (with slices::SortFunc
, for example). It is usually clearer and always faster to use the built-in string comparison operators ==
, <
, >
, and so on.
fn Repeat(s: str, mut n: int): str
Returns string that equals to concatenation of n-count s. Returns empty string is n <= 0.
fn HasPrefix(s: str, sub: str): bool
Reports string has prefix as specified substring or not.
fn HasSuffix(s: str, sub: str): bool
Reports string has suffix as specified substring or not.
fn Find(s: str, sub: str): int
Returns index of first matched item with specified substring, returns -1 if not exist any match. Starts searching at left of string to right.
fn FindLast(s: str, sub: str): int
Returns index of first matched item with specified substring, returns -1 if not exist any match. Starts searching at right of string to left.
fn FindAt(s: str, sub: str, mut i: int): int
Returns index of first matched item with specified substring, returns -1 if not exist any match. Starts searching at left of string to right. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn FindLastAt(s: str, sub: str, i: int): int
Returns index of first matched item with specified substring, returns -1 if not exist any match. Starts searching at right of string to left. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn FindFn(s: str, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at left of string to right.
fn FindFnAt(s: str, mut i: int, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at left of string to right. Starts searching s at given index. Returns -1, if i < 0.
fn FindFnLastAt(s: str, mut i: int, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at right of string to left. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn FindFnLast(s: str, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at right of string to left.
fn FindAny(s: str, runes: str): int
Returns index of first matched item with any of runes, returns -1 if not exist any match. Starts searching at right of string to left.
fn FindLastAny(s: str, runes: str): int
Returns index of first matched item with any of runes, returns -1 if not exist any match. Starts searching at right of string to left.
fn Split(s: str, sub: str, mut n: int): []str
Splits the string into the specified number of parts to the specified substring. Returns empty slice if n is equals to zero. Returns all parts if n less than zero.
fn SplitAll(s: str, sub: str): []str
Same as the Split function. But splits all parts. Basically equals to Split(s, sub, -1)
call.
fn Contains(s: str, sub: str): bool
Reports whether string includes substring.
fn ContainsByte(s: str, b: byte): bool
Reports whether string includes byte.
fn ContainsRune(s: str, r: rune): bool
Reports whether string includes rune.
fn ContainsAny(s: str, runes: str): bool
Reports whether string includes any of runes.
fn Count(s: str, sub: str): int
Counts the number of non-overlapping instances of substring in s. Returns zero if substring is empty.
fn Replace(s: str, sub: str, new: str, mut n: int): str
Replaces all substrings matching sub in the string with new. Returns same string if n is equals to zero. Replaces all matches if n less than zero.
fn ReplaceAll(s: str, sub: str, new: str): str
Same as the Replace function. But replaces all matched subs. Basically equals to Replace(s, sub, new, -1)
call.
fn Map(s: str, mapping: fn(mut rune): rune): str
Returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
fn ToLower(s: str): str
Returns s with all Unicode letters mapped to their lower case.
fn ToUpper(s: str): str
Returns s with all Unicode letters mapped to their upper case.
fn FindRune(s: str, r: rune): int
Returns index of first matched item with specified rune, returns -1 if not exist any match. Starts searching at left of string to right.
fn FindLastRune(s: str, r: rune): int
Returns index of first matched item with specified rune, returns -1 if not exist any match. Starts searching at right of string to left.
fn FindByte(s: str, b: byte): int
Returns index of first matched item with specified byte, returns -1 if not exist any match. Starts searching at left of string to right.
fn FindLastByte(s: str, b: byte): int
Returns index of first matched item with specified byte, returns -1 if not exist any match. Starts searching at right of string to left.
fn TrimLeft(s: str, cutset: str): str
Trims string by specified runes at left. Cutset should include runes to trim.
fn TrimRight(s: str, cutset: str): str
Trims string by specified runes at right. Cutset should include runes to trim.
fn Trim(s: str, cutset: str): str
Trims string by specified runes at left and right. Cutset should include runes to trim.
fn Join(parts: []str, sep: str): str
Concatenates the parts of its first argument to create a single string. The separator sep is placed between parts in the resulting string.
fn Cut(s: str, sep: str): (before: str, after: str, found: bool)
Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, "", false.
Structs
struct Builder
String builder for efficient concatenation. Optimized for single string building not for repeated use.
INFO
Implemented Traits
io::Writer
io::ByteWriter
io::RuneWriter
io::StrWriter
Methods:
static fn New(cap: int): Builder
Returns new string builder with capacity.
fn Write(mut self, b: []byte)!: (n: int)
Writes bytes to buffer. Never throws an exceptional.
fn WriteStr(mut self, s: str)!: (n: int)
Writes bytes to buffer. Never throws an exceptional.
fn WriteByte(mut self, b: byte)!
Writes byte to buffer. Never throws an exceptional.
fn WriteRune(mut self, r: rune)!: (n: int)
Writes rune into buffer. Returns written byte count. Never throws an exceptional.
fn Str(mut self): str
Returns as string, then calls the Clear
method.
fn Clear(mut self)
Clears buffer. After calling this function, write calls will allocate new buffer.
fn Len(self): int
Returns length of buffer.
fn Cap(self): int
Returns capacity of buffer.
unsafe fn Buf(mut self): []byte
Returns mutable buffer for low-level interactions.
unsafe fn SetBuf(mut self, mut buf: []byte)
Sets mutable internal buffer for low-level interactions.