The Char data type stores characters as individual, double-byte (16-bit), Unicode values; and it exposes methods for classifying the character stored in a Char variable. You can use methods such as IsDigit and IsPunctuation on a Char variable to determine its type, and other similar methods that can simplify your string validation code.
To use a character variable in your application, you must declare it with a statement such as the following one:
Dim ch As Char
ch = Convert.ToChar("A")
Code language: PHP (php)
The expression “A” represents a string, even if it contains a single character. Everything you enclose in double quotes is a string. To convert it to a character, you must cast it to the Char type. If the Strict option is off (which is the default value), you need not perform the conversion explicitly. If the Strict option is on, you must use one of the CChar() or the CType() functions, or the Convert class, to convert the single-character string in the double quotes to a character value, as shown in the preceding statement. There’s also a shorthand notation for converting one-character strings to characters — just append the c character to a single-character string:
Dim ch As Char = "A"c
Code language: PHP (php)
If you let the compiler decipher the type of the variable from its value, a single-character string will be interpreted as a string, not a Char data type. If you later assign a string value to a Char variable by using a statement such as the following, only the first character of the string will be stored in the ch variable:
ch = "ABC" ‘ the value "A" is assigned to ch!
Code language: JavaScript (javascript)
Properties
The Char class provides two trivial properties: MaxValue and MinValue. They return the largest and smallest character values you can represent with the Char data type.
Methods
The Char data type exposes several useful methods for handling characters. All the methods described here have the same syntax: They accept either a single argument, which is the character they act upon, or a string and the index of a character in the string on which they act.
GetNumericValue
This method returns a positive numeric value if called with an argument that is a digit, and the value −1 otherwise. If you call the GetNumericValue with the argument 5, it will return the numeric value 5. If you call it with the symbol @, it will return the value −1.
GetUnicodeCategory
This method returns a numeric value that is a member of the UnicodeCategory enumeration and identifies the Unicode group to which the character belongs. The Unicode groups characters into categories such as math symbols, currency symbols, and quotation marks. Look up the UnicodeCategory enumeration in the documentation for more information.
IsLetter, IsDigit, IsLetterOrDigit
These methods return a True/False value indicating whether their argument, which is a character, is a letter, decimal digit, or letter/digit, respectively. You can write an event handler by using the IsDigit method to accept numeric keystrokes and to reject letters and punctuation symbols.
We commonly use these methods to intercept keystrokes from within a control’s KeyPress (or KeyUp and KeyDown) events. The e.KeyChar property of the e argument returns the character that was pressed by the user and that fired the KeyPress event. To reject non-numeric keys as the user enters text in a TextBox control, use the event handler shown in Listing 13.1.
Listing 9.1: Rejecting Non-numeric Keystrokes
Private Sub TextBox1 KeyPress(...) _
Handles TextBox1.KeyPress
Dim c As Char
c = e.KeyChar
If Not (Char.IsDigit(c) or Char.IsControl(c)) Then
e.Handled = True
End If
End Sub
Code language: PHP (php)
This code ignores any keystrokes that don’t represent numeric digits and are not control characters. Control characters are not rejected, because we want users to be able to edit the text on the control. The Backspace key, for example, is captured by the KeyPress event, and you shouldn’t ‘‘kill’’ it. For more information on handling keystrokes from within your code, see the section ‘‘Capturing Keystrokes’’ in Chapter 6, ‘‘Basic Windows Controls.’’ If the TextBox control is allowed to accept fractional values, you should allow the period character as well, by using the following If clause:
Dim c As Char
c = e.KeyChar
If Not (Char.IsDigit(c) or c = "." or _
Char.IsControl(c)) Then
e.Handled = True
End If
Code language: PHP (php)
IsLower, IsUpper
These methods return a True/False value indicating whether the specified character is lowercase or uppercase, respectively.
IsNumber
This method returns a True/False value indicating whether the specified character is a number. The IsNumber method takes into consideration hexadecimal digits (the characters 0123456789-ABCDEF) in the same way as the IsDigit method does for decimal numbers.
IsPunctuation, IsSymbol, IsControl
These methods return a True/False value indicating whether the specified character is a punctuation mark, symbol, or control character, respectively. The Backspace and Esc keys, for example, are ISO (International Organization for Standardization) control characters.
IsSeparator
This method returns a True/False value indicating whether the character is categorized as a separator (space, new-line character, and so on).
IsWhiteSpace
This method returns a True/False value indicating whether the specified character is white space. Any sequence of spaces, tabs, line feeds, and form feeds is considered white space. Use this method along with the IsPunctuation method to remove all characters in a string that are not words.
ToLower, ToUpper
These methods convert their argument to a lowercase or uppercase character, respectively, and return it as another character.
ToString
This method converts a character to a string. It returns a single-character string, which you can use with other string-manipulation methods or functions.