NSString 验证
我需要验证用户在某些 UITextField 中输入的一些 NSString。
我需要两种验证。
1、判断一个NSString对象是否是合法的十进制数。
例如,@“100”、@“10.1”、@“0.11”都是合法的,但@“100.11.1”则不合法。
2、判断一个NSString对象是否不是一个只有空格的字符串。
例如,@“abc”、@“ab----c”都是合法的,但@“-----”、@“”则不合法。
这里的“-”表示空格“”。
如何验证这两种 NSString?
多谢!
I need to validate some NSString that user inputs in some UITextField.
There are 2 kinds of validation I need.
1st, to judge whether a NSString object is a legal decimal number.
For example, @"100", @"10.1",@"0.11", are all legal, but @"100.11.1" is not.
2nd, to judge whether a NSString object is not a space-only string.
For example, @"abc", @"ab----c", are all legal, but @"-----", @"", are not.
here "-" means a space " ".
How can I validate these 2 kinds of NSString?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于第一部分,请尝试以下
对于第二部分,请尝试以下:
For the first part try this
For the second part try this:
对于第一次验证,您可以计算小数点。零有效。任何大于 1 的值都是无效的。并且 1 始终有效(假设“.123”有效)。
对于第二次验证,您可以将空字符串
@""
与删除所有空格的原始字符串进行比较。如果它们相等,那么您的原始字符串无效。搜索周围SO。例如,NSString 中字符出现的次数可能会有所帮助。
For the first validation you can count the decimal points. Zero is valid. Anything greater than one is invalid. And one is always valid (assuming ".123" is valid).
For the second validation you can compare the empty string
@""
to the original string with all space(s) removed. If they're equal then your original string was invalid.Search around SO. For example Number of Occurrences of a Character in NSString could be helpful.