.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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • It's hardly surprising that half of small businesses fail within the first 1-5 years. It's not easy to launch a new product, single-handedly manage everything from IT to accounting, fend off the competition, and grow a customer base – all at the same time – even with a great concept. Offering awesome customer service can make the difference between a startup that flies and a startup that dies. Read this white paper to learn nine ways customer support can help you beat the competition and grow your …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds