.NET API Docs

String Constructors

The System.String class provides several constructors to create new string instances from different data sources.

Signature Description
String(char[] value) Initializes a new instance of the String class to the value indicated by an array of Unicode characters.
String(char[] value, int startIndex, int length) Initializes a new instance of the String class to the value indicated by a range of characters in an array.
String(char c, int count) Initializes a new instance of the String class consisting of a repeated character.
String(char* value) Initializes a new instance of the String class from a pointer to an array of Unicode characters. (Unsafe)
String(sbyte* value) Initializes a new instance of the String class from an array of UTF-8 encoded bytes. (Unsafe)
String(byte[] value, int startIndex, int length, Encoding enc) Initializes a new instance of the String class from a subset of a byte array using the specified encoding.
String(ReadOnlySpan<char> value) Initializes a new instance of the String class from a read‑only span of characters.
String(ReadOnlySpan<byte> value, Encoding enc) Initializes a new instance of the String class from a read‑only span of bytes using the specified encoding.

Examples

Create a string from a character array

char[] letters = { 'H', 'e', 'l', 'l', 'o' };
string s = new string(letters);
// s == "Hello"

Repeat a character

string stars = new string('*', 10);
// stars == "**********"

Construct from a byte array with UTF8 encoding

byte[] bytes = { 72, 101, 108, 108, 111 };
string s = System.Text.Encoding.UTF8.GetString(bytes);
// s == "Hello"