tags:

views:

95

answers:

3

hey guys, im just wondering if theres 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 theres 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?"
+10  A: 
var containsdigit = somestring.Any(char.IsDigit);
leppie
cool thanks. I see that if (words.ToCharArray().Any(char.IsDigit)) would return the same as if (words.IndexOfAny("0123456789".ToCharArray()) > -1)
Sinaesthetic
is there a method to check for special characters too? Im not seeing it right away
Sinaesthetic
@Sinaesthetic: It is almost functionally the same. `IsDigit` will check for some Unicode cases too. `IsNumber` does even more, like fractions.
leppie
doesnt seem to be catching special characters, so far IndexOfAny is though. I just assumed there were already be a method for this. Pretty common, no?
Sinaesthetic
@Sinaesthetic: `char.IsDigit()` also returns true for chars like `۴`, `൦` and `৮`. This may be perfect, disasterous or irrelevant depending on your case, which is a reason in itself for not having a single method to do this - there's no single correct way for all cases - along with the fact that if every commonly used one-line method string could support were added, the interface would be epic in length.
Jon Hanna
i see your point. Maybe a user class method for common special characters found on a standard keyboard would be in order? I'm in my first programming "language" class, but ive already learned a ton from picking apart what ive learned in this thread. I didn't realize that you can keep tacking on methods based on the previous method's return type
Sinaesthetic
Well, a single "standard keyboard" doesn't really exist. There's no ৮ on my keyboard, but that would be a pretty ridiculous keyboard for someone trying to write Bengali. Meanwhile, what is on my keyboard is probably slightly different to yours unless you're Irish. Finally, a lot of people won't care what's on their keyboard as long as they can access certain chars another way. So just with that one perspective you have a lot of complexity and what's needed is a string class that is relatively simple, and a lot of methods and classes that help in different ways. Which is what we have.
Jon Hanna
+3  A: 

You could use String.IndexOfAny as:

bool isNumeric = mystring.IndexOfAny("0123456789".ToCharArray()) > -1;
ho1
thanks. i went with this and it worked out great. im gonna look into leppie's suggestion as well!
Sinaesthetic
+2  A: 

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    
James