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?
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...");
}
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