Convert String to Character Array in C#

Hi Programmers, welcome to another post of ozanecare. this post i am going to write two examples to convert string to character[] in c#. To convert string string to character we should use ToCharArray method . ToCharArray is used to convert any string values into character array. we can pass index or length inside in ToCharArray. Let’s see the codes.

First Way. see below image.

2nd way. see below page.

The Combine codes of these two methods.

using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1st Wat
            string str = "Zedd We";
            char[] arr;

            arr = str.ToCharArray(); // change index
            Console.WriteLine("The Strings are: " + str);
            Console.WriteLine("After Conversion Data Type :  " + arr.GetType());

            Console.WriteLine("All Characters in Array Are : ");
            foreach (char ch in arr)
                Console.WriteLine(ch);

            // 2nd Way
            string str1 = "Tech";
            char[] arr1 = str1.ToCharArray();
            Console.WriteLine("The Data Type : " + arr1.GetType());
            Console.WriteLine("The Characters are : ");
            for (int i = 0; i < arr1.Length; i++)
            {
                char ch = arr1[i];
                Console.WriteLine(ch);
            }
        }
    }
}

Happy coding…thanks.

Post Author: adama