string
Struct · built-in type
struct string : IComparable<string>, IEquatable<string>, IHashableA string: immutable UTF-8 text.
string name = "Ann";string greeting = "Hello, " + name + "!";if (greeting.Contains("Ann") && greeting[0] == 'H') Console.WriteLine(greeting.ToUpper());Strings are values with reference semantics: assigning one shares the text (reference counted), and since a
string cannot be changed, that is never visible. == compares the text, + joins, $"x = {x}" interpolates.
s[i] is the byte at position i (a char), s[i..j] a StringSlice (a view, nothing is copied). Positions
and lengths are byte offsets.
Most methods are in the namespace String (s.Trim() is String.Trim(s)); a string converts to a
StringSlice for free.
Fields
Section titled “Fields”| Name | Description |
|---|---|
Length |
The length in bytes (not in characters: UTF-8 uses 1 to 4 bytes per character). |
Length
Section titled “Length”int LengthThe length in bytes (not in characters: UTF-8 uses 1 to 4 bytes per character).
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
FromCStr |
A string from a C string (unsafe): the bytes up to the first 0 byte are copied. |
FromBytes |
A string from bytes (UTF-8); they are copied as they are. |
FromCStr
Section titled “FromCStr”static string FromCStr(char* text)A string from a C string (unsafe): the bytes up to the first 0 byte are copied.
FromBytes
Section titled “FromBytes”static string FromBytes(uint8[] bytes)A string from bytes (UTF-8); they are copied as they are.
static string FromBytes(uint8[] bytes, int start, int count)A string from count bytes of bytes, starting at start.
Panics when the range is not inside of the array.
Methods
Section titled “Methods”| Name | Description |
|---|---|
Substring |
A copy of the count bytes from start (s[start..start + count].ToString() is the same). |
ToString |
The string itself. |
Clone |
A copy of the string in a new block of memory (e.g. to keep a part of a large text without keeping all of it). |
CStr |
A pointer to the bytes, followed by a 0 byte, for C functions (unsafe). |
AsBytes |
The bytes of the string (UTF-8) as a view, for functions that take bytes: stream.Write(text.AsBytes()); nothing is copied. |
Equals |
Whether the strings have the same bytes (also a == b). |
CompareTo |
Compares the bytes (ordinal, no locale): negative, 0 or positive. |
GetHashCode |
A hash code of the text, for Dictionary keys. |
Substring
Section titled “Substring”string Substring(int start, int count)A copy of the count bytes from start (s[start..start + count].ToString() is the same).
Panics when the range is not inside of the string.
string Substring(int start)A copy of the text from start to the end.
Panics when start is not in 0 to Length.
ToString
Section titled “ToString”string ToString()The string itself.
string Clone()A copy of the string in a new block of memory (e.g. to keep a part of a large text without keeping all of it).
char* CStr()A pointer to the bytes, followed by a 0 byte, for C functions (unsafe). It stays valid while the string
lives.
AsBytes
Section titled “AsBytes”ReadOnlySlice<uint8> AsBytes()The bytes of the string (UTF-8) as a view, for functions that take bytes: stream.Write(text.AsBytes());
nothing is copied. (A string converts to ReadOnlySlice<char> by itself.)
Equals
Section titled “Equals”bool Equals(string other)Whether the strings have the same bytes (also a == b).
CompareTo
Section titled “CompareTo”int CompareTo(string other)Compares the bytes (ordinal, no locale): negative, 0 or positive.
GetHashCode
Section titled “GetHashCode”int GetHashCode()A hash code of the text, for Dictionary keys.