;

How to Convert String Array to Char Array in C#


Tutorialsrack 19/05/2020 C#

In this article, we will learn how to convert string array to char array in C#. There are various ways to convert string array to char array. 

Method 1: By Using ToCharArray() Method

Method 1: By Using ToCharArray() Method
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Convert String Array To Char Array in C# */
        static void Main(string[] args)
        {
            string[] strArr = { "Tutorials", "Rack" };
            // Convert Element1 into Char Array
            char[] element1 = strArr[0].ToCharArray();

            Console.WriteLine("Original String Array Element 1: {0}", strArr[0]);
            Console.WriteLine("Print Element1 To After converting it into Char Array: ");
            foreach (var ch in element1)
            {
                Console.WriteLine(ch);
            }
            // Convert Element2 into Char Array
            Console.WriteLine("\nOriginal String Array Element 2: {0}", strArr[1]);
            Console.WriteLine("Print Element2 To After converting it into Char Array: ");
            char[] element2 = strArr[1].ToCharArray();
            foreach (var ch in element2)
            {
                Console.WriteLine(ch);
            }

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output
Original String Array Element 1: Tutorials
Print Element1 To After converting it into Char Array:
T
u
t
o
r
i
a
l
s
 
Original String Array Element 2: Rack
Print Element2 To After converting it into Char Array:
R
a
c
k

Method 2: By Using LINQ and ToCharArray() Method

Method 2: By Using LINQ and ToCharArray() Method
using System;
using System.Linq;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Convert String Array To Char Array Using LINQ in C# */
        static void Main(string[] args)
        {
            string[] strArr = { "Tutorials", "Rack" };

            // Convert String array into Char array list
            var charArrList = strArr.Select(x => x.ToCharArray()).ToList();

             // Print the Char Array
            for(int i = 0;i<charArrList.Count;i++)
            {
                Console.WriteLine("\nOriginal String Array Element {0}: {1}", i + 1, strArr[i].ToString());
                Console.WriteLine("Print Element {0} To After converting it into Char Array: ",i+1);
                foreach (var ch in charArrList[i])
                {
                    Console.WriteLine(ch);
                }   
            }

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output
Original String Array Element 1: Tutorials
Print Element 1 To After converting it into Char Array:
T
u
t
o
r
i
a
l
s
 
Original String Array Element 2: Rack
Print Element 2 To After converting it into Char Array:
R
a
c
k

Method 3: Using SelectMany() Method of LINQ

In this method, we used the SelectMany() method of LINQ and ToArray() converted it into the array. This method combined all the string elements and converted them into one character array.

Method 3: Using SelectMany() Method of LINQ
using System;
using System.Linq;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Convert String Array To Char Array Using LINQ in C# */
        static void Main(string[] args)
        {
            string[] strArr = { "Tutorials", "Rack" };

            // Converted all the String array elements into single character array 
            var charArrList = strArr.SelectMany(s => s).ToArray();
            Console.WriteLine("Print Element To After converting it into Char Array: ");

            foreach (var ch in charArrList)
            {
                Console.WriteLine(ch);
            }

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output
Print Element To After converting it into Char Array:
T
u
t
o
r
i
a
l
s
R
a
c
k

Method 4: Using String.Concat() and ToCharArray() Method

In this method, we used the string.Concat() method to concatenate the elements of the string array and then convert it into the char[] array using ToCharArray() method.

Method 4: Using String.Concat() and ToCharArray() Method
using System;
using System.Linq;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Convert String Array To Char Array
           Using string.Concat() andToCharArray() Method in C# */
        static void Main(string[] args)
        {
            string[] strArr = { "Tutorials", "Rack" };

            // Converted all the String array elements into single character array 
            var charArrList = string.Concat(strArr).ToCharArray();
            Console.WriteLine("Print Element To After converting it into Char Array: ");

            foreach (var ch in charArrList)
            {
                Console.WriteLine(ch);
            }

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output
Print Element To After converting it into Char Array:
T
u
t
o
r
i
a
l
s
R
a
c
k

I hope this article will help you to understand how to convert string array to char array in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags