我们如何检查字符串是否仅由数字组成。我从字符串中取出一个子字符串,并想检查它是否是一个数字子字符串。
NSString *newString = [myString substringWithRange:NSMakeRange(2,3)];
这是一种不依赖于尝试将字符串解析为数字的有限精度的方法:
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
见+[NSCharacterSet decimalDigitCharacterSet]
和-[NSString rangeOfCharacterFromSet:]
。
John Calsbeek's answer几乎是正确的,但省略了一些Unicode边缘情况。
根据documentation for decimalDigitCharacterSet
,该集合包括由Unicode分类为Nd
的所有字符。因此,他们的答案将接受,其中包括:
१
(U + 0967 DEVANAGARI DIGIT ONE)᠑
(U + 1811 MONGOLIAN DIGIT ONE)𝟙
(U + 1D7D9 MATHEMATICAL DOUBLE-STRUCK DIGIT ONE)虽然在某种意义上这是正确的 - Nd
中的每个角色都映射到十进制数字 - 这几乎肯定不是提问者所期望的。在撰写本文时,有610 code points categorized as Nd
,其中只有十个是预期的字符0
(U + 0030)到9
(U + 0039)。
要解决此问题,只需指定可接受的字符:
NSCharacterSet* notDigits =
[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
还有一个选择:
- (BOOL)isValidNumber:(NSString*)text regex:(NSString*)regex {
@try {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:text];
}
@catch (NSException *exception) {
assert(false);
return NO;
}
}
用法示例:
BOOL isValid = [self isValidNumber:@"1234" regex:@"^[0-9]+$"];
测试字符串是否为数字可能会有帮助
int i = [@"12.3" rangeOfCharacterFromSet: [ [NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet] ].location;
if (i == NSNotFound) {
//is a number
}
Swift扩展:
extension NSString {
func isNumString() -> Bool {
let numbers = NSCharacterSet(charactersInString: "0123456789.").invertedSet
let range = self.rangeOfCharacterFromSet(numbers).location
if range == NSNotFound {
return true
}
return false
} }
对于Swift 3
var onlyDigits: CharacterSet = CharacterSet.decimalDigits.inverted
if testString.rangeOfCharacter(from: onlyDigits) == nil {
// String only consist digits 0-9
}
@John Calsbeek答案的延伸,以及对@Jeff和@gyratory马戏团评论的澄清。
+ (BOOL)doesContainDigitsOnly:(NSString *)string
{
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
BOOL containsDigitsOnly = [string rangeOfCharacterFromSet:nonDigits].location == NSNotFound;
return containsDigitsOnly;
}
+ (BOOL)doesContainNonDigitsOnly:(NSString *)string
{
NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
BOOL containsNonDigitsOnly = [string rangeOfCharacterFromSet:digits].location == NSNotFound;
return containsNonDigitsOnly;
}
可以添加以下内容作为NSString
的类别方法
- (BOOL)doesContainDigitsOnly
{
NSCharacterSet *nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
BOOL containsDigitsOnly = [self rangeOfCharacterFromSet:nonDigits].location == NSNotFound;
return containsDigitsOnly;
}
- (BOOL)doesContainNonDigitsOnly
{
NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
BOOL containsNonDigitsOnly = [self rangeOfCharacterFromSet:digits].location == NSNotFound;
return containsNonDigitsOnly;
}
如果您的数字来自mixed languages,使用(或不使用)0-9位数字格式,则需要运行一个可以查找任意数字的正则表达式,接下来要将所有数字转换为0-9格式(如果您需要实际值):
// Will look for any language digits
let regex = try NSRegularExpression(pattern: "[^[:digit:]]", options: .caseInsensitive)
let digitsString = regex.stringByReplacingMatches(in: string,
options: NSRegularExpression.MatchingOptions(rawValue: 0),
range: NSMakeRange(0, string.count), withTemplate: "")
// Converting the digits to be 0-9 format
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: "EN")
let finalValue = numberFormatter.number(from: digitsString)
if let finalValue = finalValue {
let actualValue = finalValue.doubleValue
}
最简单和最可靠的方法是尝试施放Double
,如果结果是nil
- 它不能形成一个合法的数字。
let strings = ["test", "123", "123.2", "-123", "123-3", "123..22", ".02"]
let validNumbers = strings.compactMap(Double.init)
print(validNumbers)
// prints [123.0, 123.2, -123.0, 0.02]
文档中的更多信息:https://developer.apple.com/documentation/swift/double/2926277-init
我建议使用numberFromString:
类中的NSNumberFormatter方法,好像数字无效,它将返回nil;否则,它会返回一个NSNumber。
NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
BOOL isDecimal = [nf numberFromString:newString] != nil;
通过正则表达式,通过模式"^[0-9]+$"
,使用以下方法-validateString:withPattern:
进行验证。
[self validateString:"12345" withPattern:"^[0-9]+$"];
"^[0-9]+(.{1}[0-9]+)?$"
"."
。
与模式"^[0-9]{4}$"
。"."
的数字,长度在2~5之间。
与模式"^[0-9]{2,5}$"
。可以在the online web site中检查正则表达式。
辅助功能如下。
// Validate the input string with the given pattern and
// return the result as a boolean
- (BOOL)validateString:(NSString *)string withPattern:(NSString *)pattern
{
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSAssert(regex, @"Unable to create regular expression");
NSRange textRange = NSMakeRange(0, string.length);
NSRange matchRange = [regex rangeOfFirstMatchInString:string options:NSMatchingReportProgress range:textRange];
BOOL didValidate = NO;
// Did we find a matching range
if (matchRange.location != NSNotFound)
didValidate = YES;
return didValidate;
}
在操场上测试。
import UIKit
import Foundation
func validate(_ str: String, pattern: String) -> Bool {
if let range = str.range(of: pattern, options: .regularExpression) {
let result = str.substring(with: range)
print(result)
return true
}
return false
}
let a = validate("123", pattern: "^[0-9]+")
print(a)
您可以创建NSScanner并只扫描字符串:
NSDecimal decimalValue;
NSScanner *sc = [NSScanner scannerWithString:newString];
[sc scanDecimal:&decimalValue];
BOOL isDecimal = [sc isAtEnd];
查看NSScanner's documentation有更多方法可供选择。
我认为检查给定字符串中的每个字符都是数字的最简单方法可能是:
NSString *trimmedString = [newString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
if([trimmedString length])
{
NSLog(@"some characters outside of the decimal character set found");
}
else
{
NSLog(@"all characters were in the decimal character set");
}
如果要完全控制可接受的字符,请使用其他NSCharacterSet工厂方法之一。
Swift 3解决方案如果需要验证字符串只有数字:
CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: myString))
这个原始问题是关于Objective-C的,但它也是在Swift宣布之前发布的。所以,如果你是从谷歌来到这里并正在寻找一个使用Swift的解决方案,那么你去:
let testString = "12345"
let badCharacters = NSCharacterSet.decimalDigitCharacterSet().invertedSet
if testString.rangeOfCharacterFromSet(badCharacters) == nil {
print("Test string was a number")
} else {
print("Test string contained non-digit characters.")
}
要清楚,这对于字符串中的整数起作用。
根据约翰的答案,上面提到了一个小助手类别:
在.h文件中
@interface NSString (NumberChecking)
+(bool)isNumber:(NSString *)string;
@end
在.m文件中
#import "NSString+NumberChecking.h"
@implementation NSString (NumberChecking)
+(bool)isNumber {
if([self rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location == NSNotFound) {
return YES;
}else {
return NO;
}
}
@end
用法:
#import "NSString+NumberChecking.h"
if([someString isNumber]) {
NSLog(@"is a number");
}else {
NSLog(@"not a number");
}
Swift 3解决方案可能如下:
extension String {
var doubleValue:Double? {
return NumberFormatter().number(from:self)?.doubleValue
}
var integerValue:Int? {
return NumberFormatter().number(from:self)?.intValue
}
var isNumber:Bool {
get {
let badCharacters = NSCharacterSet.decimalDigits.inverted
return (self.rangeOfCharacter(from: badCharacters) == nil)
}
}
}