c sharp

How to Convert Char Array to String in C#?

There are multiple functions in C# that can convert a char array to a string. This article covers three different methods of converting a char array to a string; the string() method, using the Join() method, and using the Concat() method.

Table of Contents

Using string() Method

The simplest and most straightforward way to convert a char array to a string in C# is to use the string() method. This function accepts a character array as an argument and produces a string as its output.

Syntax

string str = new string(character_array);

Example Code

The following C# code explains how one can convert a char array to a string using the string() method:

using System;
using System.Text;
public class myClass{
    static string GetString(char[] arr) {
        // string() is used to convert
        // the char array to a string
        string s = new string(arr);        
        return s;
    }
    static void Main(string[] args) {
        // given character array
        char[] arr = {'L', 'i', 'n', 'u', 'x', 'h', 'i', 'n', 't'};
        // call the GetString method and
        // pass the char array as an argument
        string str = GetString(arr);
        // print the resulting string
        Console.WriteLine(str);
    }
}

The code starts by defining a static method named GetString() which takes a char array as input and returns a string representation of it. To get output as a string the GetString() uses the string() constructor.

In the Main() method, a char array with the values ‘L’, ‘i’, ‘n’, ‘u’, ‘x’, ‘h’, ‘i’, ‘n’, and ‘t’ is created. Then, the GetString() method is called with this char array as an argument, and the resulting string is stored in the variable str. Next, the Console.WriteLine() will output a string on the console.

When the program runs, it will print the string Linuxhint to the console.

Using Join() Method

Another way to convert a char array to a string in C# is to use the Join() method. This method takes two parameters: a separator string and an array of strings. It concatenates the strings in the array and inserts the separator string between them.

Syntax

string str = string.Join("", character_array);

Example Code
The following given code demonstrates how to utilize the Join() method to convert a character array to a string.

using System;
using System.Text;
public class myClass{  
    static string ConvertToString(char[] arr)
    {
        string s = string.Join("", arr);
        return s;
    }  
    static void Main(string[] args)
    {
        // given character array
        char[] arr = {'L', 'i', 'n', 'u',
        'x', 'h', 'i', 'n', 't'};      
        // function calling
        string str = ConvertToString(arr);     
        Console.WriteLine(str);
    }
}

The program defines a class named myClass with two methods: ConvertToString() and Main(). The ConvertToString() function is a static method that takes a character array as input and returns a string by joining all the characters of the array using an empty string as a separator.

The Main() is also a static method that initializes a character array with the characters Linuxhint. It then calls the ConvertToString() method, passes the array as an argument, and stores the returned string in the str variable. Next, it prints the value of str using the Console.WriteLine().

When executed, the program outputs the string Linuxhint to the console.

Using Concat() Method

The third way to convert a char array to a string in C# is to use the Concat() method. The function accepts two or more strings as input parameters and combines them into a single string.

Syntax

string str = string.Concat(character_array);

Example Code
The given C# code explains the use of string.Concat() to convert a character array to a string.

using System;
using System.Text;
public class Program{  
    static string ConvertToString(char[] arr)
    {
        string s = string.Concat(arr);     
        return s;
    }  
    static void Main(string[] args)
    {
        // given character array
        char[] arr = {'L', 'i', 'n', 'u',
        'x', 'h', 'i', 'n', 't'};      
        // function calling
        string str = ConvertToString(arr);     
        // printing output
        Console.WriteLine(str);
    }
}

The code defines a static method named ConvertToString() that takes a char array as input and returns a string by concatenating all the characters in the array.

In the Main() method, a character array is initialized with the characters ‘L’, ‘i’, ‘n’, ‘u’, ‘x’, ‘h’, ‘i’, ‘n’, and ‘t’. Then the ConvertToString() method is called with this character array as an argument to convert it into a string. The Console.WriteLine() will display the output.

After running, the code string Linuxhint will appear on the console.

Conclusion

Converting a char array to a string in C# is a common task in string manipulation. Multiple methods are available in C# to convert a char array to a string. The string() method is the simplest way of converting any input char array to a string output. The Join() method is useful if we need to insert a separator string between the characters, while the Concat() method is useful if we need to concatenate multiple strings together.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.