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

前端 未结 6 1427
一向
一向 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条回答
  •  猫巷女王i
    2021-02-05 10:54

    You can think of writing a generic function like the following for this:

    BOOL isNumericI(NSString *s)
    {
       NSUInteger len = [s length];
       NSUInteger i;
       BOOL status = NO;
    
       for(i=0; i < len; i++)
       {
           unichar singlechar = [s characterAtIndex: i];
           if ( (singlechar == ' ') && (!status) )
           {
             continue;
           }
           if ( ( singlechar == '+' ||
                  singlechar == '-' ) && (!status) ) { status=YES; continue; }
           if ( ( singlechar >= '0' ) &&
                ( singlechar <= '9' ) )
           {
              status = YES;
           } else {
              return NO;
           }
       }
       return (i == len) && status;
    }
    

提交回复
热议问题