Convert Char Array to StringConvert a char array into a string. Use the string constructor and StringBuilder.
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.
Shows a string array
Notes, string conversions. We examine the C# string constructor. The StringBuilder class can also be used—but it is not ideal for this purpose.
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
Detail We assign the 3 chars (in the char array) to letters. Recall that char is 2 bytes. Next we use the string() constructor.
Detail This constructor is the same as a regular constructor such as new Form(), but we can use a lowercase first letter.
Shows a string 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}"); } }
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.
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}"); } }
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.