如何检查NSString是否为数字 [英] How to check if NSString is numeric or not
本文介绍了如何检查NSString是否为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
iphone如何检查字符串是否仅为数字
Possible Duplicate:
iphone how to check that a string is numeric only
我有一个NSString,然后我要检查字符串是否为数字.
I have one NSString, then i want check the string is number or not.
我的意思是
NSString *val = @"5555" ;
if(val isNumber ){
return true;
}else{
retun false;
}
如何在Objective C中做到这一点?
How can I do this in Objective C?
推荐答案
使用[NSNumberFormatter numberFromString: s]
.如果指定的字符串不是数字,则返回nil.您可以配置NSNumberFormatter为您的特定方案定义数字".
Use [NSNumberFormatter numberFromString: s]
. It returns nil if the specified string is non-numeric. You can configure the NSNumberFormatter to define "numeric" for your particular scenario.
#import <Foundation/Foundation.h>
int
main(int argc, char* argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLocale *l_en = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSLocale *l_de = [[NSLocale alloc] initWithLocaleIdentifier: @"de_DE"];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setLocale: l_en];
NSLog(@"returned: %@", [f numberFromString: @"1.234"]);
[f setAllowsFloats: NO];
NSLog(@"returned: %@", [f numberFromString: @"1.234"]);
[f setAllowsFloats: YES];
NSLog(@"returned: %@", [f numberFromString: @"1,234"]);
[f setLocale: l_de];
NSLog(@"returned: %@", [f numberFromString: @"1,234"]);
[l_en release];
[l_de release];
[f release];
[pool release];
}
这篇关于如何检查NSString是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文