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

前端 未结 6 1345
一向
一向 2021-02-05 10:46

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

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 11:13

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

提交回复
热议问题