Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - how do I check if a string is numeric or not? [duplicate]

Possible Duplicate:
iPhone how to check that a string is numeric only

I am trying to look up some basic functionality in Objective C, but can't seem to locate the basic answer.

What I really need is to check

if ( some_string is numeric )
{

}

I would assume there would just be some built in function for that. No?

I am looking at this StackOverflow question How to check if NSString is numeric or not and they seem to be doing things the very complicated way.

Isn't there some basic check I can do in one simple line?

Thanks!

like image 963
GeekedOut Avatar asked Jul 21 '12 00:07

GeekedOut


1 Answers

Isn't there some basic check I can do in one simple line?

No.

NSScanner *scanner = [NSScanner scannerWithString:testString];
BOOL isNumeric = [scanner scanInteger:NULL] && [scanner isAtEnd];

If you want to allow decimal digits exchange scanInteger: by scanDouble:.

like image 76
Nikolai Ruhe Avatar answered Oct 06 '22 23:10

Nikolai Ruhe