Home
Search
C# Convert Char Array to StringConvert a char array into a string. Use the string constructor and StringBuilder.
dot net perls
Convert char array, string. A char array can be converted into a string. The syntax to perform this conversion can be confusing at first. The simplest approach uses the string constructor.
Notes, string conversions. We examine the C# string constructor. The StringBuilder class can also be used—but it is not ideal for this purpose.
Constructor
StringBuilder
First example. Here we convert the char array. This approach is interesting because you see the lowercase string type has a constructor. Things like ints do not have constructors.
And Unlike int or char, a string is a reference type. We initialize a char array.
Char Array
Example We assign the 3 chars (in the char array) to letters. Recall that char is 2 bytes. Next we use the string() constructor.
Char
Constructor This constructor is the same as a regular constructor such as new Form(), but we can use a lowercase first letter.
C# program that converts char array
using System; class Program { static void Main() { // Create 3-character array. char[] array = new char[3]; array[0] = 'c'; array[1] = 'a'; array[2] = 't'; // Create string from array. string result = new string(array); Console.WriteLine($"STRING: {result}"); } }
STRING: cat
StringBuilder. Here we use the StringBuilder type to convert a char array to a string. We append each char to the StringBuilder, and then call() ToString.
Info We can transform the chars before we append them to the StringBuilder, or add other string data as we go along.
C# program that uses StringBuilder to convert to string
using System; using System.Text; class Program { static void Main() { // Create 3-character array. char[] array = { 'c', 'a', 't' }; // Loop over the array with foreach, and append to a StringBuilder. StringBuilder builder = new StringBuilder(); foreach (char value in array) { builder.Append(value); } string result = builder.ToString(); Console.WriteLine($"STRINGBUILDER RESULT: {result}"); } }
STRINGBUILDER RESULT: cat
Notes, string keyword. The lowercase "string" type is an alias for the String reference type. Strings reference external data—this is allocated on the managed heap.
Performance. The new string constructor is overall very efficient when compared to other approaches. It is the fastest way to make a new string in many cases.
String Constructor
Tip It is much faster than manually appending characters to your string, or using StringBuilder.
Research. Microsoft states that the String Constructor(Char[]) "Initializes a new instance of the String class to the value indicated by an array of Unicode characters."
And If you pass the constructor null, you get an empty string (not a null one).
A summary. With a constructor, we created a new string object from an array. You can run the example console program to prove the method works correctly.
Home
© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.