C #: Is there a way to find a string for a number without using regular expressions?

Hey guys, they are just wondering if there is a way to check if a string contains any numeric digits in it without using regular expressions. I thought to just break it into an array and run a search on it, but something tells me it's easier.

//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?"
+5
source share
3 answers

You can use String.IndexOfAnyas:

bool isNumeric = mystring.IndexOfAny("0123456789".ToCharArray()) > -1;
+3
source
var containsdigit = somestring.Any(char.IsDigit);
+9
source

LINQ Char.IsNumber, .

public static class StringExt
{
    public static bool ContainsNumber(this string str)
    {
        return str.Any(c => Char.IsNumber(c)); 
    }
}

:

//pseudocodestring 
string str = "The number 4";
If (aString.ContainsNumber())
    enter validation    
+2

All Articles