#50 – Static Methods of System.Char

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
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: