iOS - 如何检查字符串是否为数字?
可能重复:
iPhone如何检查字符串是否只是数字
我正在尝试查找Objective C中的一些基本功能,但似乎无法找到基本答案。
I am trying to look up some basic functionality in Objective C, but can't seem to locate the basic answer.
什么我真的需要检查
if ( some_string is numeric )
{
}
我认为会有一些内置功能。不是吗?
I would assume there would just be some built in function for that. No?
我正在看这个StackOverflow问题如何检查NSString是否为数字,他们似乎是以非常复杂的方式做事。
I am looking at this StackOverflow question How to check if NSString is numeric or not and they seem to be doing things the very complicated way.
我可以在一个简单的行中做一些基本检查吗?
Isn't there some basic check I can do in one simple line?
谢谢!
我可以在一个简单的行中进行一些基本检查吗?
Isn't there some basic check I can do in one simple line?
否。
NSScanner *scanner = [NSScanner scannerWithString:testString];
BOOL isNumeric = [scanner scanInteger:NULL] && [scanner isAtEnd];
如果你想允许十进制数字交换 scanInteger:
by scanDouble:
。
If you want to allow decimal digits exchange scanInteger:
by scanDouble:
.