Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a char is digit or not in Objective-C?

Tags:

objective-c

I need to check if a char is digit or not.

NSString *strTest=@"Test55";
char c =[strTest characterAtIndex:4];

I need to find out if 'c' is a digit or not. How can I implement this check in Objective-C?

like image 936
syam Avatar asked Feb 16 '10 07:02

syam


1 Answers

Note: The return value for characterAtIndex: is not a char, but a unichar. So casting like this can be dangerous...

An alternative code would be:

NSString *strTest = @"Test55";
unichar c = [strTest characterAtIndex:4];
NSCharacterSet *numericSet = [NSCharacterSet decimalDigitCharacterSet];
if ([numericSet characterIsMember:c]) {
    NSLog(@"Congrats, it is a number...");
}
like image 157
Laurent Etiemble Avatar answered Oct 11 '22 08:10

Laurent Etiemble