How do I convert string to character value type?

Category: System, viewed: 1K time(s).

The example below show you how to convert a string value to its equivalent character type value. We can use the Char.Parse() method or get a character from specific index of a string literal.

using System;

namespace Kodecsharp.Example.System
{
    public class StringToChar
    {
        public static void Main(string[] args)
        {
            string charString = "a";

            //
            // Convert string value to its equivalent unicode character.
            //
            char c = Char.Parse(charString);
            Console.WriteLine("C = " + c);

            string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < alphabets.Length; i++)
            {
                //
                // Get a character on the specific index of a string.
                //
                char alphabet = alphabets[i];
                Console.Write(alphabet);
            }

            Console.ReadLine();
        }
    }
}
Powered by Disqus