IMG-LOGO

How to convert string to char array in C#?

andy - 02 Apr, 2021 3115 Views 0 Comment

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.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.

Free Open Source .Net CMS

Are you looking for an open source .Net CMS for your site? When we say open source it means you can get full access to the source code without having to pay a cent.