Thursday, 4 October 2007

System.Char (char) at rescue...

Sometimes is necessery to check if a string (or portion of it) contains numbers, letters or whatever else...
For the ones that "sometimes" (me included...) writes what the BCL already gives you, go to .NET documentation or MSDN online and be happy with System.Char static methods!
All the the following suggestion must be applied to a loop like that:

string s = "Hello 123 123.999 $%&! :_"; //here the input to check
foreach (char c in s)
if (char.******(c))

You want check for...

...integer values? -> char.IsDigit(char_var)

...floating point? -> char.IsDigit(char_var) || char_var == '.'

...plain letters? -> char.IsLetter(char_var)

...everything is white space -> char.IsWhiteSpace(char_var)

...symbols (+, -, etc..) -> char.IsSysmbol(char_var)

and many interesting method to test, parse or check your characters sequence!

No comments:

Post a Comment