How do I determine the type of Character?

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

This example demonstrate how to define the kind of value contained in a char, whether the value is a digit, number, letter, symbol, punctuation, separator, control, whitespace or a surrogate character.

To determine the character type we can use statics method of System.Char class such as:

  • Char.isDigit: Any decimal digit in the range 0–9 in all Unicode locales.
  • Char.isNumber: Any decimal digit or hexadecimal digit; this includes digits such as superscripts, subscripts, etc.
  • Char.isLetter: Any alphabetic letter.
  • Char.isSymbol: Any mathematical, currency, or other symbol character. Includes characters that modify surrounding characters.
  • Char.isPunctuation: Any punctuation character.
  • Char.isSeparator: A space separating words, a line separator, or a paragraph separator.
  • Char.isControl: A control code in the ranges \U007F,\U0000–\U001F, and \U0080–\U009F.
  • Char.isWhiteSpace: Any space character.
  • Char.isSurrogate: Any surrogate character in the range \UD800–\UDFFF.

Let's see the example code below:

using System;

namespace Kodecsharp.Example.System
{
    public class DeterminingCharType
    {
        public static void Main(string[] args)
        {
            char c = '?';

            if (Char.IsDigit(c))
            {
                Console.WriteLine("Char " + c + " is digit");
            }
            else if (Char.IsNumber(c))
            {
                Console.WriteLine("Char " + c + " is number");
            }
            else if (Char.IsSeparator(c))
            {
                Console.WriteLine("Char " + c + " is separator");
            }
            else if (Char.IsSymbol(c))
            {
                Console.WriteLine("Char " + c + " is symbol");
            }
            else if (Char.IsControl(c))
            {
                Console.WriteLine("Char " + c + " is control");
            }
            else if (Char.IsLetter(c))
            {
                Console.WriteLine("Char " + c + " is letter");
            }
            else if (Char.IsPunctuation(c))
            {
                Console.WriteLine("Char " + c + " is punctuation");
            }
            else if (Char.IsSurrogate(c))
            {
                Console.WriteLine("Char " + c + " is surrogate");
            }
            else if (Char.IsWhiteSpace(c))
            {
                Console.WriteLine("Char " + c + " is whitespace");
            }

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