Convert Char Array to String in C#

C# | Convert char[] to string: Here, we are going to learn how to convert char array to string in C#? By IncludeHelp Last updated : April 11, 2023

Given a character array and we have to convert it into a string.

char[] to string in C#

In C#, we can convert a character array to the string, for this – we can create a new string from the character array.

Syntax to Convert Char Array to String

    string string_object = new string(character_array);

It accepts a character array as an arguments and creates new string.

Sample Input/Output

Input:

char[] char_arr = { 'H', 'e', 'l', 'l', 'o'};

Creating string:
string str = new string(char_arr);

Output:
str = "Hello"

C# Example to Convert Char Array (char[]) to String

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      char[] char_arr = {'H','e','l','l','o'};

      //converting char[] to string
      string str = new string(char_arr);

      //printing string
      Console.WriteLine("str = " + str);

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

str = Hello



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.