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();
}
}
}
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();
}
}
}
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();
}
}
}
Comments