Is there a way to check to see if a string contains any numeric digits in it without using regex? I was thinking of just splitting it into an array and running a search on that, but something tells me there is an easier way:
//pseudocode
string aString = "The number 4"
If (aString contains a number) Then enter validation loop
Else return to main
//output
"The string contains a number. Are you sure you want to continue?"
var containsdigit = somestring.Any(char.IsDigit);
You could use String.IndexOfAny
as:
bool isNumeric = mystring.IndexOfAny("0123456789".ToCharArray()) > -1;
You could create an extension method for string and use a combination of LINQ and the Char.IsNumber function e.g.
public static class StringExt
{
public static bool ContainsNumber(this string str)
{
return str.Any(c => Char.IsNumber(c));
}
}
Then your logic would look like:
//pseudocodestring
string str = "The number 4";
If (aString.ContainsNumber())
enter validation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With