Skip to content
CShift0.32

String

The methods of strings. The compiler forwards the string methods it does not know itself to this namespace:

"a,b".Contains(",") // is String.Contains("a,b", ",")
string.Join(", ", parts) // is String.Join(", ", parts)

The functions take a StringSlice (a string converts to one for free, and s[i..j] is one), so they work on parts of a string without copying; String.Trim, String.Split and String.Substring return slices as well. Copy a result with .ToString() to keep it as a string.

Strings are UTF-8. Positions and lengths are byte offsets (like s.Length and s[i]). Case conversion only handles ASCII letters.

Name Description
IsNullOrEmpty Whether s is empty (a null string is empty too).
Equals Whether a and b have the same bytes (a == b).
GetHashCode A hash code of s (FNV-1a), for Dictionary keys: equal strings have equal codes.
CompareTo Compares a and b by their UTF-8 bytes (ordinal, no locale).
IndexOf The position of the first occurrence of value in s at or after start.
LastIndexOf The position of the last occurrence of the character value in s.
Contains Whether value occurs in s.
StartsWith Whether s starts with prefix.
EndsWith Whether s ends with suffix.
Substring The part of s from start to the end, as a view: s[start..].
IsSpace Whether c is a space, a tab or a line break (\n, \r): the white space that String.Trim removes.
Trim s without the white space at both ends (a view of s, nothing is copied).
TrimStart s without the white space at the start (a view).
TrimEnd s without the white space at the end (a view).
PadLeft s filled up to width characters (bytes) with spaces on the left: "7".PadLeft(3) is " 7".
PadRight s filled up to width characters (bytes) with spaces on the right.
ToUpper s with the ASCII letters in upper case.
ToLower s with the ASCII letters in lower case.
Replace s with every occurrence of oldValue replaced by newValue.
Repeat s repeated count times: "ab".Repeat(3) is "ababab".
Split The parts of s between the occurrences of separator, as views of s (nothing is copied): "a,,b".Split(",") is ["a", "", "b"].
Join The parts joined into one string, with separator between them: string.Join(", ", names).
ParseInt64 The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).
ParseInt The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).
ParseDouble The floating point number in s ("3.5", "-1e-3", "inf", …; the C library’s strtod, without spaces around it).
bool IsNullOrEmpty(StringSlice s)

Whether s is empty (a null string is empty too).

bool Equals(string a, string b)

Whether a and b have the same bytes (a == b).

bool Equals(StringSlice a, StringSlice b)

Whether a and b have the same bytes (a == b).

int GetHashCode(string s)

A hash code of s (FNV-1a), for Dictionary keys: equal strings have equal codes.

int GetHashCode(StringSlice s)

A hash code of s (FNV-1a): the same as for a string with the same text.

int CompareTo(string a, string b)

Compares a and b by their UTF-8 bytes (ordinal, no locale).

Returns -1 if a comes first, 0 if they are equal, 1 if b comes first.

int CompareTo(StringSlice a, StringSlice b)

Compares a and b by their UTF-8 bytes (ordinal, no locale), like strings.

Returns -1 if a comes first, 0 if they are equal, 1 if b comes first.

int IndexOf(StringSlice s, StringSlice value, int start)

The position of the first occurrence of value in s at or after start.

Returns -1 if there is none.

int IndexOf(StringSlice s, StringSlice value)

The position of the first occurrence of value in s.

Returns -1 if there is none.

int IndexOf(StringSlice s, char value)

The position of the first occurrence of the character value in s.

Returns -1 if there is none.

int IndexOf(StringSlice s, char value, int start)

The position of the first occurrence of the character value in s at or after start.

Returns -1 if there is none.

int LastIndexOf(StringSlice s, char value)

The position of the last occurrence of the character value in s.

Returns -1 if there is none.

bool Contains(StringSlice s, StringSlice value)

Whether value occurs in s.

bool Contains(StringSlice s, char value)

Whether the character value occurs in s.

bool StartsWith(StringSlice s, StringSlice prefix)

Whether s starts with prefix.

bool EndsWith(StringSlice s, StringSlice suffix)

Whether s ends with suffix.

StringSlice Substring(StringSlice s, int start)

The part of s from start to the end, as a view: s[start..]. (string.Substring of a string is built in and copies.)

Since 0.06

StringSlice Substring(StringSlice s, int start, int count)

The count bytes of s from start, as a view: s[start..start + count].

Since 0.06

bool IsSpace(char c)

Whether c is a space, a tab or a line break (\n, \r): the white space that String.Trim removes.

StringSlice Trim(StringSlice s)

s without the white space at both ends (a view of s, nothing is copied).

StringSlice Trim(StringSlice s, char c)

s without the character c at both ends (a view): "--a-b--".Trim('-') is "a-b".

Since 0.32

StringSlice TrimStart(StringSlice s)

s without the white space at the start (a view).

Since 0.04

StringSlice TrimStart(StringSlice s, char c)

s without the character c at the start (a view): "007".TrimStart('0') is "7".

Since 0.04

StringSlice TrimEnd(StringSlice s)

s without the white space at the end (a view).

Since 0.04

StringSlice TrimEnd(StringSlice s, char c)

s without the character c at the end (a view): "1.500".TrimEnd('0') is "1.5".

Since 0.04

string PadLeft(StringSlice s, int width)

s filled up to width characters (bytes) with spaces on the left: "7".PadLeft(3) is " 7".

Since 0.04

string PadLeft(StringSlice s, int width, char fill)

s filled up to width characters (bytes) with fill on the left: "7".PadLeft(3, '0') is "007".

Since 0.04

string PadRight(StringSlice s, int width)

s filled up to width characters (bytes) with spaces on the right.

Since 0.04

string PadRight(StringSlice s, int width, char fill)

s filled up to width characters (bytes) with fill on the right.

Since 0.04

string ToUpper(StringSlice s)

s with the ASCII letters in upper case.

string ToLower(StringSlice s)

s with the ASCII letters in lower case.

string Replace(StringSlice s, StringSlice oldValue, StringSlice newValue)

s with every occurrence of oldValue replaced by newValue.

Returns s unchanged if oldValue is empty.

string Repeat(StringSlice s, int count)

s repeated count times: "ab".Repeat(3) is "ababab".

StringSlice[] Split(StringSlice s, StringSlice separator)

The parts of s between the occurrences of separator, as views of s (nothing is copied): "a,,b".Split(",") is ["a", "", "b"].

StringSlice[] Split(StringSlice s, char separator)

The parts of s between the occurrences of the character separator, as views of s.

string Join(StringSlice separator, string[] parts)

The parts joined into one string, with separator between them: string.Join(", ", names).

string Join(StringSlice separator, StringSlice[] parts)

The parts (e.g. the result of String.Split) joined into one string, with separator between them.

ParseError<int64> ParseInt64(StringSlice s)

The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).

Errors

ParseError<int> ParseInt(StringSlice s)

The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).

Errors

ParseError<double> ParseDouble(StringSlice s)

The floating point number in s ("3.5", "-1e-3", "inf", …; the C library’s strtod, without spaces around it).

Errors