NSString *test = @"example";
NSString *test2 = @"ex12am243ple";
是否有任何简单的方法可以确定哪个字符串包含数字(0-9),哪个字符串不包含数字?
谢谢。
最佳答案
if([testString rangeOfCharacterFromSet:characterSetOfNumbers].location == NSNotFound)
{
//there are no numbers in this string
}
else
{
//there is at least 1 number in this string
}
附:您可以查看 NSCharacterSet
的文档以获取可用的,但您可能想要的是 decimalDigitCharacterSet
因此您可以使用 [NSCharacterSet DecimalDigitCharacterSet]
代替上面代码中的“characterSetOfNumbers”。
关于iphone - 我的一些 NSString 是字母数字的。如何判断哪些包含数字,哪些不包含数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590292/