In this article, we will learn how to convert a char array to string in C#. There are various ways to convert the char array to string. Here, we can convert char array to string in three ways: the first way is by using string constructor, the second way is by using the StringBuilder and the third way is by using the string.join() method.
Method 1: Using String Constructor
In this method, we used the String constructor and pass the char array to the constructor.
Method 1: Using String Constructor
using System;
namespace Tutorialsrack
{
class Program
{
/* How To Convert Char Array To String in C# */
static void Main(string[] args)
{
// Convert char array to string
char[] chars = new char[15];
chars[0] = 'T';
chars[1] = 'u';
chars[2] = 't';
chars[3] = 'o';
chars[4] = 'r';
chars[5] = 'i';
chars[6] = 'a';
chars[7] = 'l';
chars[8] = 's';
chars[9] = 'r';
chars[10] = 'a';
chars[11] = 'c';
chars[12] = 'k';
string charsStr1 = new string(chars);
string charsStr2 = new string(new char[]
{'T','u','t','o','r','i','a','l','s','r','a','c','k','.','c','o','m'});
Console.WriteLine("Chars to string: {0}", charsStr1);
Console.WriteLine("Chars to string: {0}", charsStr2);
//Hit ENTER to exit the program
Console.ReadKey();
}
}
}
Output
Chars to string: Tutorialsrack
Chars to string: Tutorialsrack.com
Method 2: Using StringBuilder
In this method, we create the StringBuilder object and iterate the char array values by using a loop and append each element to the StringBuilder and then call the ToString() method. And the StringBuilderclass belongs to System.Text namespace.
Method 2: Using StringBuilder
using System;
using System.Text;
namespace Tutorialsrack
{
class Program
{
/* How To Convert Char Array To String using StringBuilder in C# */
static void Main(string[] args)
{
// Convert char array to string
char[] chars = new char[15];
chars[0] = 'T';
chars[1] = 'u';
chars[2] = 't';
chars[3] = 'o';
chars[4] = 'r';
chars[5] = 'i';
chars[6] = 'a';
chars[7] = 'l';
chars[8] = 's';
chars[9] = 'r';
chars[10] = 'a';
chars[11] = 'c';
chars[12] = 'k';
// Loop over the array with foreach, and append to a StringBuilder.
StringBuilder sb = new StringBuilder();
foreach (var ch in chars)
{
sb.Append(ch);
}
var Output = sb.ToString();
// Print the Output
Console.WriteLine("Chars to string: {0}", Output);
// Hit ENTER to exit the program
Console.ReadKey();
}
}
}
Output
Chars to string: Tutorialsrack
Method 3: Using String.Join() Method
In this method, we used the String.Join() method to convert char array to string.
Method 3: Using String.Join() Method
using System;
namespace Tutorialsrack
{
class Program
{
/* How To Convert Char Array To String using String.Join() in C# */
static void Main(string[] args)
{
// Convert char array to string
char[] chars = new char[]{'T','u','t','o','r','i','a','l','s','r','a','c','k','.','c','o','m'};
Console.WriteLine("Chars to string: {0}", string.Join("", chars));
//Hit ENTER to exit the program
Console.ReadKey();
}
}
}
Output
Chars to string: Tutorialsrack.com
I hope this article will help you to understand how to convert char array to string in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments