module watt.text.string

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 { }
alias StrArg

Helper alias for string args that are scoped.

alias StrArrayArg

Helper alias for string array args that are scoped.

fn split(s: scope (StrArg), delimiter: dchar) string[]

Divide s into an array of strings.

Examples

split("a=b", '=') ["a", "b"]
split("a = b", '=') ["a ", " b"]
split("a=b", '@') ["a=b"]
fn split(s: scope (StrArg), delimiter: scope (StrArg)) string[]

Divide s into an array of strings.

fn splitLines(s: scope (StrArg)) string[]

Get an array with an element for each line in s.

Example

splitLines("a\nb\nc");  // ["a", "b", "c"]
fn strip(str: scope (StrArg)) string

Remove whitespace before and after str.

Whitespace is defined by isWhite.

Examples

strip("  apple  ") -> "apple"
strip("  apple  pie  ") -> "apple pie"
fn stripLeft(str: scope (StrArg)) string

Remove leading whitespace from str.

Whitespace is defined by isWhite.

fn stripRight(str: scope (StrArg)) string

Remove trailing whitespace.

Whitespace is defined by isWhite.

fn count(str: scope (StrArg), c: dchar) size_t

Count how many times c occurs in s.

fn indexOf(str: scope (StrArg), c: dchar) ptrdiff_t

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.

fn lastIndexOf(str: scope (StrArg), c: dchar) ptrdiff_t

Find a character in a string, starting from the end.

Return

The index of the last place c occurs in str, -1 otherwise.

fn indexOf(str: scope (StrArg), sub: scope (StrArg)) ptrdiff_t

Find a string in a string.

If the substring sub occurs in s, return the index where it occurs. Otherwise, return -1.

fn indexOf(ss: scope (StrArrayArg), str: scope (StrArg)) ptrdiff_t

Find a string in an array of strings.

Return

The index in which s first occurs in ss, or -1.

fn replace(str: scope (StrArg), from: scope (StrArg), to: scope (StrArg)) string

Replace instances of a string in a string with another.

Parameters

str

The string to search for instances to replace.

from

The string to replace if found.

to

The string to replace from with.

Return

A copy of s with occurences of from replaced with to, or s on its own if from does not occur.

fn startsWith(str: scope (StrArg), beginnings: scope (StrArrayArg)) i32

Return

A non-zero value if str starts with one of the strings given by beginnings.

fn endsWith(str: scope (StrArg), ends: scope (StrArrayArg)) i32

Return

A non-zero value if str ends with one of the strings given by ends.

fn join(ss: scope (StrArrayArg), sep: scope (StrArg)) string

Join an array of strings into one string, separated by sep.

Example

join(["a", "b"], "-");  // "a-b"
fn copyOrPass(str: scope (StrArg)) string

Helper function, that copies a string if needed.