【发布时间】:2012-08-27 14:55:35
【问题描述】:
可能重复:
NSString is integer?
我正在寻找一个字符串是否只有数字。基本上:
if (string is integer):
#do whatever
else:
#error
我该怎么做?谢谢!
【问题讨论】:
可能重复:
NSString is integer?
我正在寻找一个字符串是否只有数字。基本上:
if (string is integer):
#do whatever
else:
#error
我该怎么做?谢谢!
【问题讨论】:
我用过[NSCharacterSet decimalDigitCharacterSet]。绝对运作良好。
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
【讨论】:
-(BOOL)isANumber:(NSString *)string{
NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^[0-9]+$'"];
return [numberPredicate evaluateWithObject:string];
}
【讨论】: