Check if a string has at least one number in it using LINQ
Solution 1:
"abc3def".Any(c => char.IsDigit(c));
Update: as @Cipher pointed out, it can actually be made even shorter:
"abc3def".Any(char.IsDigit);
Solution 2:
Try this
public static bool HasNumber(this string input) {
return input.Where(x => Char.IsDigit(x)).Any();
}
Usage
string x = GetTheString();
if ( x.HasNumber() ) {
...
}
Solution 3:
or possible using Regex:
string input = "123 find if this has a number";
bool containsNum = Regex.IsMatch(input, @"\d");
if (containsNum)
{
//Do Something
}