Members of System Char

VB 2008 textual data is represented by the intrinsic String and Char keywords, which are simple shorthand notations for System.String and System.Char, both of which are Unicode under the hood. As you most certainly already know, a string is a contiguous set of characters (e.g., "Hello"). The Char data type can represent a single slot in a String type (e.g., "H").

By default, when you define textual data within double quotes, the VB 2008 compiler assumes you are defining a full-blown String type. However, to build a single character string literal that should be typed as a Char, place the character between double quotes and tack on a single "c" after the closing quote. Doing so ensures that the double-quoted text literal is indeed represented as a System.Char, rather than a System.String:

Dim myChar As Char = "a"c

Note When you enable Option Strict (described in the section "Understanding Option Strict" later in this chapter) for your project, the VB 2008 compiler demands that you tack on the c suffix to a Char data type when assigning a value.

The System.Char type provides you with a great deal of functionality beyond the ability to hold a single point of character data. Using the shared methods of System.Char, you are able to determine whether a given character is numerical, alphabetical, a point of punctuation, or whatnot. To illustrate, update Main() with the following statements:

' Fun with System.Char.

Dim myChar As Char = "a"c

Console.WriteLine("Char.IsDigit('a'): {0}", Char.IsDigit(myChar)) Console.WriteLine("Char.IsLetter('a'): {0}", Char.IsLetter(myChar))

Console.WriteLine("Char.IsWhiteSpace('Hello There', 5): {0}", _

Char.IsWhiteSpace("Hello There", 5)) Console.WriteLine("Char.IsWhiteSpace('Hello There', 6): {0}", _

Char.IsWhiteSpace("Hello There", 6)) Console.WriteLine("Char.IsPunctuation('?'): {0}", _ Char.IsPunctuation("?"c))

As illustrated in the previous code snippet, the members of System.Char have two calling conventions: a single character or a string with a numerical index that specifies the position of the character to test.

Was this article helpful?

0 0

Post a comment