本文介绍了NSString 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要验证用户在某些 UITextField 中输入的某些 NSString.
I need to validate some NSString that user inputs in some UITextField.
我需要两种验证.
1、判断一个NSString对象是否是合法的十进制数.
1st, to judge whether a NSString object is a legal decimal number.
例如@"100"、@"10.1"、@"0.11"都是合法的,而@"100.11.1"就不是.
For example, @"100", @"10.1",@"0.11", are all legal, but @"100.11.1" is not.
2、判断一个 NSString 对象是否不是只有空格的字符串.
2nd, to judge whether a NSString object is not a space-only string.
例如@"abc"、@"ab----c"都是合法的,而@"-----"、@""则不是.
For example, @"abc", @"ab----c", are all legal, but @"-----", @"", are not.
这里的-"表示空格".
here "-" means a space " ".
如何验证这两种 NSString?
How can I validate these 2 kinds of NSString?
非常感谢!
推荐答案
第一部分试试这个
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setAllowsFloats:YES];
[formatter setDecimalSeparator:@"."];
NSNumber *number = [formatter numberFromString: inputString];
if(number == nil)
{
//invalid input string
}
第二部分试试这个:
NSString *str = [inputString stringByReplacingOccurrencesOfString:@" " withString:@""];
if([str length] == 0)
{
//invalid input string
}
这篇关于NSString 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!