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.
Functions
Section titled “Functions”| 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). |
IsNullOrEmpty
Section titled “IsNullOrEmpty”bool IsNullOrEmpty(StringSlice s)Whether s is empty (a null string is empty too).
Equals
Section titled “Equals”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).
GetHashCode
Section titled “GetHashCode”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.
CompareTo
Section titled “CompareTo”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.
IndexOf
Section titled “IndexOf”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.
LastIndexOf
Section titled “LastIndexOf”int LastIndexOf(StringSlice s, char value)The position of the last occurrence of the character value in s.
Returns -1 if there is none.
Contains
Section titled “Contains”bool Contains(StringSlice s, StringSlice value)Whether value occurs in s.
bool Contains(StringSlice s, char value)Whether the character value occurs in s.
StartsWith
Section titled “StartsWith”bool StartsWith(StringSlice s, StringSlice prefix)Whether s starts with prefix.
EndsWith
Section titled “EndsWith”bool EndsWith(StringSlice s, StringSlice suffix)Whether s ends with suffix.
Substring
Section titled “Substring”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
IsSpace
Section titled “IsSpace”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
TrimStart
Section titled “TrimStart”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
TrimEnd
Section titled “TrimEnd”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
PadLeft
Section titled “PadLeft”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
PadRight
Section titled “PadRight”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
ToUpper
Section titled “ToUpper”string ToUpper(StringSlice s)s with the ASCII letters in upper case.
ToLower
Section titled “ToLower”string ToLower(StringSlice s)s with the ASCII letters in lower case.
Replace
Section titled “Replace”string Replace(StringSlice s, StringSlice oldValue, StringSlice newValue)s with every occurrence of oldValue replaced by newValue.
Returns s unchanged if oldValue is empty.
Repeat
Section titled “Repeat”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.
ParseInt64
Section titled “ParseInt64”ParseError<int64> ParseInt64(StringSlice s)The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).
Errors
ParseError.Invalid:sis not an integer.ParseError.OutOfRange: the number does not fit into anint64.
ParseInt
Section titled “ParseInt”ParseError<int> ParseInt(StringSlice s)The integer in s: decimal digits with an optional + or - sign, nothing else (no spaces).
Errors
ParseError.Invalid:sis not an integer.ParseError.OutOfRange: the number does not fit into anint.
ParseDouble
Section titled “ParseDouble”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
ParseError.Invalid:sis not a number.