How to convert an array of characters into a string in C#?

CsharpProgrammingServer Side Programming

Let us first set an array of 5 characters −

char[] ch = new char[15];
ch[0] = 'T';
ch[1] = 'r';
ch[2] = 'i';
ch[3] = 'c';
ch[4] = 'k';

Now convert them into a string −

string str = new string(ch);

Here is the complete code −

Example

Using System;
class Program {
   static void Main() {
      char[] ch = new char[15];
      ch[0] = 'T';
      ch[1] = 'r';
      ch[2] = 'i';
      ch[3] = 'c';
      ch[4] = 'k';
      // converting to string
      string str = new string(ch);
      Console.WriteLine(str);
   }
}
raja
Published on 21-Aug-2018 17:43:07
Advertisements