Simple functions for working with string
values.
The functions that return a string, will return a copy -- the original will not be changed.
Example
a := " apple ";
b := strip(a);
assert(a == " apple ");
assert(b == "apple");
Code Map
//! Simple functions for working with string values.
module watt.text.string;
//! Helper alias for string args that are scoped.
alias StrArg = scope (const(scope (char)[]);
//! Helper alias for string array args that are scoped.
alias StrArrayArg = scope (scope (const(scope (char)[])[]);
//! Divide s into an array of strings.
fn split(s: scope (StrArg), delimiter: dchar) string[] { }
//! Divide s into an array of strings.
fn split(s: scope (StrArg), delimiter: scope (StrArg)) string[] { }
//! Get an array with an element for each line in s.
fn splitLines(s: scope (StrArg)) string[] { }
//! Get an array with an element for each line in s.
fn splitLines(s: string) string[] { }
//! Remove whitespace before and after str.
fn strip(str: scope (StrArg)) string { }
//! Remove leading whitespace from str.
fn stripLeft(str: scope (StrArg)) string { }
//! Remove trailing whitespace.
fn stripRight(str: scope (StrArg)) string { }
//! Count how many times c occurs in s.
fn count(str: scope (StrArg), c: dchar) size_t { }
//! Find a character in a string.
fn indexOf(str: scope (StrArg), c: dchar) ptrdiff_t { }
//! Find a character in a string, starting from the end.
fn lastIndexOf(str: scope (StrArg), c: dchar) ptrdiff_t { }
//! Find a string in a string.
fn indexOf(str: scope (StrArg), sub: scope (StrArg)) ptrdiff_t { }
//! Find a string in an array of strings.
fn indexOf(ss: scope (StrArrayArg), str: scope (StrArg)) ptrdiff_t { }
//! Replace instances of a string in a string with another.
fn replace(str: scope (StrArg), from: scope (StrArg), to: scope (StrArg)) string { }
fn startsWith(str: scope (StrArg), beginnings: scope (StrArrayArg)) i32 { }
fn endsWith(str: scope (StrArg), ends: scope (StrArrayArg)) i32 { }
//! Join an array of strings into one string, separated by sep.
fn join(ss: scope (StrArrayArg), sep: scope (StrArg)) string { }
Helper alias for string args that are scoped.
Helper alias for string array args that are scoped.
Divide s
into an array of string
s.
Examples
split("a=b", '=') ["a", "b"]
split("a = b", '=') ["a ", " b"]
split("a=b", '@') ["a=b"]
Divide s
into an array of string
s.
Get an array with an element for each line in s
.
Example
splitLines("a\nb\nc"); // ["a", "b", "c"]
Remove whitespace before and after str
.
Whitespace is defined by isWhite.
Examples
strip(" apple ") -> "apple"
strip(" apple pie ") -> "apple pie"
Remove leading whitespace from str
.
Whitespace is defined by isWhite.
Remove trailing whitespace.
Whitespace is defined by isWhite.
Count how many times c
occurs in s
.
Find a character in a string
.
Return
The index of the first place c
occurs in str
,
or -1
if it doesn't at all.
Find a character in a string
, starting from the end.
Return
The index of the last place c
occurs in str
, -1 otherwise.
Find a string
in a string
.
If the substring sub
occurs in s
, return the index where it occurs.
Otherwise, return -1
.
Find a string
in an array of string
s.
Return
The index in which s
first occurs in ss
, or -1
.
Replace instances of a string
in a string
with another.
Parameters
str |
The |
from |
The |
to |
The |
Return
A copy of s
with occurences of from
replaced with to
, or s
on its own
if from
does not occur.
Return
A non-zero value if str
starts with one of the strings given by beginnings
.
Return
A non-zero value if str
ends with one of the strings given by ends
.
Join an array of strings into one string, separated by sep
.
Example
join(["a", "b"], "-"); // "a-b"
Helper function, that copies a string if needed.