.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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds