Published on: 02 Apr, 2021

How to convert string to char array in C#?

Tumblr Digg Google Plus

If you need to convert string to char array in .Net C# you can use the built-in function in the string library called ToCharArray.

See below code example in C#.

string letters = "abcdefghijklmnopqrstuvwxyz";
char[] charLetters = letters.ToCharArray();

You can see the sample result in Windows console below. We converted into char array and printout one by one.

using System;
using System.Collections.Generic;
namespace ConsoleApp
{
    class Program
    {
        public static void Main(string[] args)
        {
            string letters = "abcdefghijklmnopqrstuvwxyz";
            char[] charLetters = letters.ToCharArray();
            for(var i = 0; i < charLetters.Length; i++)
            {
                Console.WriteLine(string.Format("Array Index: {0} is {1}", i, charLetters[i]));
            }
            Console.Read();
        }
    }
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.