#50 – Static Methods of System.Char
August 6, 2010 Leave a comment
The System.Char class has a large number of static methods that serve as utility methods for getting information about individual characters.
Here are some examples:
char c1 = '2';
double d1 = char.GetNumericValue(c1); // Convert '2' to 2.0
UnicodeCategory cat = char.GetUnicodeCategory('a'); // LowercaseLetter
cat = char.GetUnicodeCategory('+'); // MathSymbol
cat = char.GetUnicodeCategory('6'); // DecimalDigitNumber
// Check for control characters
bool isCtrl = char.IsControl('a'); // false
isCtrl = char.IsControl('\t'); // true
// Check for digits
bool isDigit = char.IsDigit('a'); // false
isDigit = char.IsDigit('3'); // true
// Check for letters
bool isLetter = char.IsLetter('%'); // false
isLetter = char.IsLetter('P'); // true
isLetter = char.IsLetter('ǽ'); // true
bool lord = char.IsLetterOrDigit('j'); // true
// Check for lower/upper case
bool low = char.IsLower('j'); // true
low = char.IsLower('Y'); // false
bool upper = char.IsUpper('Ǻ'); // true
// Check for numbers
bool isnum = char.IsNumber('4'); // true
isnum = char.IsNumber('௧'); // true
isnum = char.IsNumber('X'); // false
// Other
bool ispunc = char.IsPunctuation('?'); // true
bool issep = char.IsSeparator(' '); // true
bool issymbol = char.IsSymbol('$'); // true
// Unicode
bool issur = char.IsSurrogate('\xd840'); // true
bool islow = char.IsLowSurrogate('\xd840'); // false
bool ishigh = char.IsHighSurrogate('\xd840'); // true
// Conversion
char upp = char.ToUpper('a'); // A
char lower = char.ToLower('a'); // a
lower = char.ToLower('Y'); // y