There are a several built-in static methods on the System.Char structure to determine the kind of character. The table below lists the static methods.
|
The following examples demonstrate how to use the methods in a function to return the kind of a character. First, create an enumeration to define the various types of characters:
public enum enumCharKind
{
Control,
Digit,
Letter,
Number,
Punctuation,
Separator,
Surrogate,
Symbol,
Whitespace,
Unknown
}
Next, create a method that contains the logic to determine the type of a character and to return a enumCharKind enumeration value indicating that type:
public static enumCharKind GetCharKind(char theChar) { if (Char.IsControl(theChar)) { return enumCharKind.Control; } else if (Char.IsDigit(theChar)) { return enumCharKind.Digit; } else if (Char.IsLetter(theChar)) { return enumCharKind.Letter; } else if (Char.IsNumber(theChar)) { return enumCharKind.Number; } else if (Char.IsPunctuation(theChar)) { return enumCharKind.Punctuation; } else if (Char.IsSeparator(theChar)) { return enumCharKind.Separator; } else if (Char.IsSurrogate(theChar)) { return enumCharKind.Surrogate; } else if (Char.IsSymbol(theChar)) { return enumCharKindSymbol; } else if (Char.IsWhiteSpace(theChar)) { return enumCharKind.Whitespace; } else { return enumCharKind.Unknown; } }
If, however, a character in a string needs to be evaluated, use the overloaded static methods on the Char structure. The following code modifies the GetCharKind method to accept a string variable and a character position in that string. The character position determines which character in the string is evaluated:
public static enumCharKind GetCharKindInString(string theString, int charPosition) { if (Char.IsControl(theString, charPosition)) { return enumCharKind.Control; } else if (Char.IsDigit(theString, charPosition)) { return enumCharKind.Digit; } else if (Char.IsLetter(theString, charPosition)) { return enumCharKind.Letter; } else if (Char.IsNumber(theString, charPosition)) { return enumCharKind.Number; } else if (Char.IsPunctuation(theString, charPosition)) { return enumCharKind.Punctuation; } else if (Char.IsSeparator(theString, charPosition)) { return enumCharKind.Separator; } else if (Char.IsSurrogate(theString, charPosition)) { return enumCharKind.Surrogate; } else if (Char.IsSymbol(theString, charPosition)) { return enumCharKind.Symbol; } else if (Char.IsWhiteSpace(theString, charPosition)) { return enumCharKind.Whitespace; } else { return enumCharKind.Unknown; } }
The GetCharKind method accepts a character as a parameter and performs a series of tests on that character using the Char type’s built-in static methods. An enumeration of all the different types of characters is defined and is returned by the GetCharKind method.
The following code example determines whether the fifth character (the charPosition parameter is zero-based) in the string is a digit:
if (GetCharKind("abcdefg", 4) == enumCharKindDigit) {...}
Popularity: 1% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply