检查 NSString 是否为整数 [英] Checking if NSString is Integer
本文介绍了检查 NSString 是否为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
NSString 是整数吗?
我正在查看一个字符串是否只有数字.基本上:
I'm looking to see if a string is ONLY numbers. Basically:
if (string is integer):
#do whatever
else:
#error
我该怎么做?谢谢!
推荐答案
我用过[NSCharacterSet decimalDigitCharacterSet]
.绝对运作良好.
I have used [NSCharacterSet decimalDigitCharacterSet]
. Definitely works well.
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
这篇关于检查 NSString 是否为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文