.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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds