iOS - 如何检查字符串是否为数字? [英] iOS - how do I check if a string is numeric or not?

查看:107
本文介绍了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:.

这篇关于iOS - 如何检查字符串是否为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆