Character test utility

Every now and then I have to decide if a character satisfies a predicate (char.IsPunctuation, char.IsSymbol and so on) found on the System.Char class. The MSDN pages are of limited help plus I need to browse each predicate. Therefore I wrote a small piece of code that, aside from the fact that it’s quite simple and has nothing spectacular, tests a set of characters through all these predicates.

Basically this is a reference for me, in order to avoid rewriting this over and over again, but since it could be helpful to others too I decided to publish it here.


using System;
using System.Linq;

namespace CharTest
{
    internal class Program
    {
        static void Main()
        {
            var charPredicates = new Predicate<char>[]
            {
                char.IsControl, 
                char.IsDigit, 
                char.IsHighSurrogate, 
                char.IsLetter,
                char.IsLetterOrDigit,
                char.IsLowSurrogate, 
                char.IsLower, 
                char.IsNumber, 
                char.IsPunctuation, 
                char.IsSeparator, 
                char.IsSurrogate, 
                char.IsSymbol, 
                char.IsUpper, 
                char.IsWhiteSpace
            };

            Console.WriteLine("Enter an empty line to exit");
            Console.WriteLine();
            Console.WriteLine();

            string line;
            do
            {
                line = Console.ReadLine();
                var chars = line.Distinct().ToArray();
                for (var i = 0; i < chars.Count(); i++)
                {
                    var ch = chars[i];
                    Console.WriteLine();
                    Console.WriteLine("Distinct character " + (i + 1) + " : '" + ch + "'");
                    Console.WriteLine("-----------------------------------------");
                    foreach (var predicate in charPredicates)
                    {

                        Console.Write(predicate.Method.Name);
                        Console.SetCursorPosition(20, Console.CursorTop);
                        Console.WriteLine(predicate(ch));
                    }
                }
            }
            while (line != "");
        }
    }
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP Like Button Plugin by Free WordPress Templates