I would like to know what the easiest and shortest LINQ query is to return true if a string contains any number character in it.
Thanks Jobi Joy
I would like to know what the easiest and shortest LINQ query is to return true if a string contains any number character in it.
Thanks Jobi Joy
Try this
public static bool HasNumber(this string input) {
return input.Where(x => Char.IsDigit(x)).Any();
}
Usage
string x = GetTheString();
if ( x.HasNumber() ) {
...
}
"abc3def".Any(c => char.IsDigit(c));
Update: as @Cipher pointed out, it can actually be made even shorter:
"abc3def".Any(char.IsDigit);