3.2. Boost package for string manipulation library
The STRINGS_BOOST module extends string handling with splitting, joining, padding, character replacement, and edit distance computation.
All functions and symbols are in “strings_boost” module, use require to get access to it.
require daslib/strings_boost
Example:
require daslib/strings_boost
[export]
def main() {
let parts = split("one,two,three", ",")
print("split: {parts}\n")
print("join: {join(parts, " | ")}\n")
print("[{wide("hello", 10)}]\n")
print("distance: {levenshtein_distance("kitten", "sitting")}\n")
}
// output:
// split: [[ one; two; three]]
// join: one | two | three
// [hello ]
// distance: 3
3.2.1. Split and join
3.2.1.1. join
- strings_boost::join(it: auto; separator: string) : auto()
Joins the elements of an iterable into a single string using the specified separator.
- Arguments
it : auto
separator : string implicit
- strings_boost::join(iterable: array<auto(TT)>; separator: string; blk: block<(var writer:StringBuilderWriter;elem:TT):void>) : string()
- strings_boost::join(it: iterator<auto(TT)>; separator: string) : auto()
- strings_boost::join(iterable: iterator<auto(TT)>; separator: string; blk: block<(var writer:StringBuilderWriter;elem:TT):void>) : string()
- strings_boost::join(iterable: auto(TT)[]; separator: string; blk: block<(var writer:StringBuilderWriter;elem:TT):void>) : string()
3.2.1.2. split
- strings_boost::split(text: string; delim: string) : array<string>()
Splits a string by the specified delimiter string and returns an array of substrings.
- Arguments
text : string implicit
delim : string implicit
- strings_boost::split(text: string; delim: string; blk: block<(arg:array<string>#):auto>) : auto()
3.2.1.3. split_by_chars
- strings_boost::split_by_chars(text: string; delim: string) : array<string>()
Splits a string by the specified delimiter characters and returns an array of substrings.
- Arguments
text : string implicit
delim : string implicit
- strings_boost::split_by_chars(text: string; delim: string; blk: block<(arg:array<string>#):auto>) : auto()
3.2.2. Formatting
- strings_boost::capitalize(str: string) : string()
Returns a copy of the string with the first character converted to uppercase. The rest of the string is unchanged.
- Arguments
str : string
- strings_boost::pad_left(str: string; width: int; ch: int = 32) : string()
Pads the string with the character ch on the left to reach the specified minimum width. If the string is already at least width characters, it is returned unchanged.
- Arguments
str : string
width : int
ch : int
- strings_boost::pad_right(str: string; width: int; ch: int = 32) : string()
Pads the string with the character ch on the right to reach the specified minimum width. If the string is already at least width characters, it is returned unchanged.
- Arguments
str : string
width : int
ch : int
- strings_boost::wide(text: string; width: int) : string()
Pads the string with trailing spaces to reach the specified minimum width.
- Arguments
text : string implicit
width : int
3.2.3. Queries and comparisons
- strings_boost::contains(str: string; sub: string) : bool()
Returns true if sub is found anywhere within str.
- Arguments
str : string
sub : string
- strings_boost::count(str: string; sub: string) : int()
Counts non-overlapping occurrences of sub in str. Returns 0 if sub is empty or not found.
- Arguments
str : string
sub : string
3.2.3.1. eq
- strings_boost::eq(b: das_string; a: string) : auto()
Compares a string with a das_string for equality, returning true if they match.
- Arguments
b : das_string
a : string implicit
- strings_boost::eq(a: string; b: das_string) : auto()
- strings_boost::is_character_at(foo: array<uint8>; idx: int; ch: int) : auto()
Returns true if the byte at the specified index in the array equals the given character code.
- Arguments
foo : array<uint8> implicit
idx : int
ch : int
- strings_boost::is_null_or_whitespace(str: string) : bool()
Returns true if the string is null, empty, or contains only whitespace characters (space, tab, CR, LF).
- Arguments
str : string
3.2.4. Search
3.2.4.1. last_index_of
- strings_boost::last_index_of(str: string; sub: string; start: int) : int()
Returns the index of the last occurrence of sub in str searching only up to position start (exclusive), or -1 if not found.
- Arguments
str : string
sub : string
start : int
- strings_boost::last_index_of(str: string; sub: string) : int()
3.2.5. Replace
- strings_boost::replace_multiple(source: string; replaces: array<tuple<text:string;replacement:string>>) : string()
Applies multiple find-and-replace substitutions to a string in a single pass.
- Arguments
source : string
replaces : array<tuple<text:string;replacement:string>>
3.2.6. Prefix and suffix
- strings_boost::trim_prefix(str: string; prefix: string) : string()
Removes prefix from the beginning of str if present. Returns the string unchanged if it does not start with prefix.
- Arguments
str : string
prefix : string
- strings_boost::trim_suffix(str: string; suffix: string) : string()
Removes suffix from the end of str if present. Returns the string unchanged if it does not end with suffix.
- Arguments
str : string
suffix : string
3.2.7. Levenshtein distance
- strings_boost::levenshtein_distance(s: string; t: string) : int()
Computes the Levenshtein edit distance between two strings.
- Arguments
s : string implicit
t : string implicit
- strings_boost::levenshtein_distance_fast(s: string; t: string) : int()
Computes the Levenshtein edit distance between two strings using an optimized algorithm.
- Arguments
s : string implicit
t : string implicit