April 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

.NET Tip: Determining the Type of a Character Variable

  • September 12, 2008
  • By Jay Miller
  • Send Email »
  • More Articles »

How do you determine what type a character variable holds? Is it a number, a letter, a punctuation mark, or somthing 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.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel