Convert char array to string in C#

Convert char array to string in C#

C# Convert char[] to a string: Here, we will figure out how to change over scorch array to string in C#?

Given a character array and we need to change over it into a string.

char[] to string in C#

In C#, we can change over a character array to the string, for this – we can make another string from the character array.

Syntax:

    string string_object = new string(character_array);

It acknowledges a character array as contention and makes new string.

Example:

    Input:
    char[] char_arr = { 'H', 'e', 'l', 'l', 'o'};
    Creating string:
    string str = new string(char_arr);
    Output:
    str = "Hello"

Code:

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

Recent Posts

error: Alert: Content is protected!!