.NET Tip: Determining the Type of a Character Variable

How do you determine what type a character variable holds? Is it a number, a letter, a punctuation mark, or something else? One answer is to take advantage of the built-in static methods of the System.Char data type directly. You could use Char.IsDigit(), for example, to determine whether a character variable contained a digit in the range 0-9. There are other static methods that you can use to determine if a character is a letter, symbol, punctuation, and so forth. In some instances, using these static methods may be your best choice. In my case, however, I wanted something a little different. To keep my application code clean, I wanted a single method call that could return the type of character a variable contained. One method to do this is to create an extension method for the System.Char data type that returns the desired information.

First, create an enumeration that contains an entry for each type of character you are interested in. In this case, I have included most of the possible character types that System.Char supports. See the documentation for System.Char for more complete information. Next, create an extension method for the System.Char data type that performs the work. This method uses the static methods on the System.Char data type to determine the type of character the variable contains and returns the corresponding value from the CharType enumeration.

public enum CharType
{
   Control,
   Digit,
   Letter,
   Punctuation,
   Separator,
   Symbol,
   Whitespace,
   Unknown
}

public static CharType GetCharType(this char c)
{
   if (Char.IsControl(c))
      return CharType.Control;
   else if (Char.IsDigit(c))
      return CharType.Digit;
   else if (Char.IsLetter(c))
      return CharType.Letter;
   else if (Char.IsPunctuation(c))
      return CharType.Punctuation;
   else if (Char.IsSeparator(c))
      return CharType.Separator;
   else if (Char.IsSymbol(c))
      return CharType.Symbol;
   else if (Char.IsWhiteSpace(c))
      return CharType.Whitespace;
   else
      return CharType.Unknown;
}

To test this extension method, create a Form and add a TextBox (txtChar) and Label (lblChar) to the Form. Add a handler for the KeyPress event of txtChar. Here is what the code for the KeyPress handler looks like.

private void txtChar_KeyPress(object sender, KeyPressEventArgs e)
{
   lblChar.Text = e.KeyChar.GetCharType().ToString();
   switch (e.KeyChar.GetCharType())
   {
      case CharType.Digit:
         e.KeyChar = '~';
         break;
   }
}

The first line in the KeyPress handler displays the name of the type of character typed in the lblChar control. I decided that I did not want to display any numbers in the txtChar, so any time a digit character type is detected it should be replaced with a tilde. The switch statement takes care of providing this behavior.

Now, making decisions in your programs based on the types of characters in your data can be easily accomplished. To take this one step further, you may want to create an extension method that lets you check that type of character at a given index in a string.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • On-demand Event Event Date: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds