How to check if NSString is contains a numeric value?

前端 未结 7 2051
走了就别回头了
走了就别回头了 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:23

    It's very simple.

    + (BOOL)isStringNumeric:(NSString *)text
    {
        NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
        NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:text];        
        return [alphaNums isSupersetOfSet:inStringSet];
    }
    

提交回复
热议问题