热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查NSString是整数还是双精度-checkiftheNSStringisintegerordouble

howcanIcheckiftheNSString*valIhavehasaIntegerorfloat??Therawapproachistolookfo

how can I check if the NSString *val I have has a Integer or float ?? The raw approach is to look for "." character..but is there a more graceful way to do this?

如何检查NSString *val是否有整数或浮点数?原始的方法是寻找“。”但有没有更优雅的方式来做到这一点呢?

1 个解决方案

#1


8  

First, try [NSScanner scanInt:]&& [NSScanner isAtEnd]. If it returns YES, then you have an int. scanInt will scan forward as long as it can interpret the stream as an int. If isAtEnd is YES, then the entire string could be interpreted as an int (so you have an int).

首先,尝试[NSScanner:]& [NSScanner isAtEnd]。如果它返回YES,那么您有一个int. scanInt将扫描,只要它能将流解释为int,如果isAtEnd是YES,那么整个字符串可以被解释为int(所以您有一个int)。

Otherwise, try [NSScanner scanDouble:]. If it returns YES, then you have a double.

否则,尝试[NSScanner scanDouble:]。如果它返回YES,那么你有一个double。

If both return NO, then you don't have either.

如果两者都返回NO,那么两者都没有。


推荐阅读
author-avatar
我是小白我有不烦的梦想
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有