Recipe 5.24. Determining a Character s Type

Recipe 5.24. Determining a Character's Type

Problem

You want to determine if a character is a letter, a digit, whitespace, or any of several other types before processing it further. This can avoid unexpected exceptions, or prevent having to use an exception on purpose to help determine the type of a character.

Solution

Sample code folder: Chapter 05\CharType

Use one of the many type-testing shared methods of the Char object.

Discussion

The Char object includes several methods that let you determine if a character is part of a larger general category of characters, such as the set of digits. The following code shows many of these in operation while it creates a handy listing of the types of all characters in the ASCII range 0 to 127:

` Dim result As New System.Text.StringBuilder Dim counter As Integer Dim testChar As Char Dim testHex As String Dim soFar As Integer ' ----- Scan through the first half of the ASCII chart. For counter = 0 To 127    ' ----- What character will we test this time?    testChar = Chr(counter)    testHex = "\x" & Hex(counter)    If Char.IsLetter(testChar) Then _       result.AppendLine(testHex & " IsLetter")    If Char.IsControl(testChar) Then _       result.AppendLine(testHex & " IsControl")    If Char.IsDigit(testChar) Then _       result.AppendLine(testHex & " IsDigit")    If Char.IsLetterOrDigit(testChar) Then _       result.AppendLine(testHex & " IsLetterOrDigit")    If Char.IsLower(testChar) Then _       result.AppendLine(testHex & " IsLower")    If Char.IsNumber(testChar) Then _       result.AppendLine(testHex & " IsNumber")    If Char.IsPunctuation(testChar) Then _       result.AppendLine(testHex & " IsPunctuation")    If Char.IsSeparator(testChar) Then _       result.AppendLine(testHex & " IsSeparator")    If Char.IsSymbol(testChar) Then _       result.AppendLine(testHex & " IsSymbol")    If Char.IsUpper(testChar) Then _       result.AppendLine(testHex & " IsUpper")    If Char.IsWhiteSpace(testChar) Then _       result.AppendLine(testHex & " IsWhiteSpace")        ' ----- Display results in blocks of 16 characters.    soFar += 1    If ((soFar Mod 16) = 0) Then       MsgBox(result.ToString( ))       result.Length = 0    End If Next counter `

The message box displays the results for 16 characters at a time. Figure 5-26 shows the output displayed for the first set of characters, and Figure 5-27 shows the results for characters with hexadecimal values in the range of some of the ASCII digits and letters.

Figure 5-27. Characters in the range hexadecimal 30 to hexadecimal 3F are mostly digits, letters, and numbers

Note that many characters fall into several categories. For example, the "0" (zero) character with hexadecimal value 30passes the test for IsDigit, IsLetterOrDigit, and IsNumber.

Recipe 5.22 includes examples of verifying logical data within strings, instead of the individual characters.

Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon