Home » C#.Net

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#?
Submitted by IncludeHelp, on February 10, 2019

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:

    string string_object = new string(character_array);

It accepts a character array as an arguments and creates 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.