How to check if NSString is contains a numeric value?

前端 未结 7 2049
走了就别回头了
走了就别回头了 2021-01-31 09:09

I have a string that is being generate from a formula, however I only want to use the string as long as all of its characters are numeric, if not that I want to do something dif

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 09:29

    Like this:

    - (void)isNumeric:(NSString *)code{
    
        NSScanner *ns = [NSScanner scannerWithString:code];
        float the_value;
        if ( [ns scanFloat:&the_value] )
        {
            NSLog(@"INSIDE IF");
            // do something with `the_value` if you like
        }
        else {
        NSLog(@"OUTSIDE IF");
        }
    }
    

提交回复
热议问题