C# program to convert a string to a character array

C# program to convert a string to a character array:

In this post, we will learn how to convert a string to a character array in C#. The String class provides a method called ToCharArray that can be used to get the characters of a string as an array in C#.

Definition of String.ToCharArray:

The String.ToCharArray method is defined as like below:

public char[] ToCharArray (int start, int length);

public char[] ToCharArray ();

Here,

  • start is the starting position of the string to get the character array.
  • length is the length of the character array.

It returns a character array, char[]. The parameter start and length are optional. If we don’t pass these values, it will return one character array holding all characters of the string.

Exception in String.ToCharArray:

This method might throw ArgumentOutOfRangeException if:

  • start or length is negative.
  • start + length is invalid, or it is greater than the length of the string.

Method 1: String to character array by using String.ToCharArray:

Let’s take an example of String.ToCharArray:

using System;

namespace dotnetsample
{
    class Program
    {
        
        static void Main(string[] args)
        {
            String GivenString;
            char[] Arr;
            
            Console.WriteLine("Enter a string: ");
            GivenString = Console.ReadLine();

            Arr = GivenString.ToCharArray();

            foreach(char c in Arr){
                Console.Write(c+" ");
            }
        }
    }
}

For this example,

  • GivenString is a string and Arr is a character array.
  • It takes a string as an input from the user and stores that in the GivenString variable.
  • It calls ToCharArray to get the array of characters and stores it in Arr.
  • The foreach loop is iterating through the characters of the array and prints these on console.

If you run this program, it will print output as like below:

Enter a string: 
Hello World
H e l l o   W o r l d

Method 2: By iterating through the characters of the string one by one:

Another way is to iterate through the characters of the string one by one and add each character to an array.

using System;

namespace dotnetsample
{
    class Program
    {

        static void Main(string[] args)
        {
            String GivenString;

            Console.WriteLine("Enter a string: ");
            GivenString = Console.ReadLine();

            char[] Arr = new char[GivenString.Length];

            for (int i = 0; i < GivenString.Length; i++)
            {
                Arr[i] = GivenString[i];
            }

            foreach (char c in Arr)
            {
                Console.Write(c + " ");
            }
        }
    }
}

Here,

  • The Arr is created after the user entered the string. The size of the array is equal to the size of the string, or the total count of characters of the string.
  • The for loop iterates through the characters of the string one by one and adds each character to the array Arr.
  • The foreach loop is printing the characters of the array.

It will give similar output.

Enter a string: 
Hello World
H e l l o   W o r l d

You might also like: