如何检查字符串中的字符但从第 4 个字符到第 7 个字符?
How to check characters from string but starting from 4th character to 7th character?
例如,如果我有 NSString *a = "com42FA";
如何检查字符串包含数字、十六进制或数字但检查从字符 4 开始到字符 7.
一个简单的解决方案是正则表达式
模式在字符集0-9
、A-F
和a-f
中搜索4个字符。
显式范围 NSMakeRange(3, 4)
搜索字符 4 - 7(location
参数从零开始)。
NSString *a = @"com42FA";
NSString *pattern = @"[0-9A-Fa-f]{4}";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSRange range = [regex rangeOfFirstMatchInString:a options:0 range:NSMakeRange(3, 4)];
BOOL hexNumberFound = range.location != NSNotFound;
NSLog(@"%d", hexNumberFound);
您可以使用 NSScanner
,它在几乎所有情况下都比正则表达式更快:
NSString *initialString = @"com42FA";
NSScanner *scanner = [NSScanner scannerWithString:initialString];
// Setup the scanner. Depends on your needs
scanner.caseSensitive = NO;
scanner.charactersToBeSkipped = nil;
// Specify the location to start scan from
scanner.scanLocation = 3;
// Actual scanning, note that I'm checking that the scanner at the end
// to understand whether it scanned up to the end of the string
unsigned long long scannedNumber = 0;
BOOL success = [scanner scanHexLongLong:&scannedNumber] && scanner.isAtEnd;
if (success) {
NSLog(@"%llu", scannedNumber); // 17146
<...>
}
作为第三种选择(无论如何在撰写本文时!)一个简单的循环将处理这个:
NSString *testString = @"com42FA";
NSCharacterSet *hexDigits = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
NSUInteger pos = 3;
BOOL isValid = YES;
while (pos <= 6 && isValid) isValid = [hexDigits characterIsMember:[testString characterAtIndex:pos++]];
只要发现无效数字就循环检查并停止。
附录
由于其他答案提出了性能问题,并不是说对于这么小的任务它可能是个问题,我提供以下更快的变体:
NSString *testString = @"com42FA";
NSUInteger pos = 3;
BOOL isValid = YES;
while (pos <= 6 && isValid) isValid = isxdigit([testString characterAtIndex:pos++]);
这使用标准 C 库函数 isxdigit()
,避免了 NSCharacterSet
创建和方法调用的需要。 (这可能也不是最快的选择,但在这一点之后可读性可能会受到影响。)
例如,如果我有 NSString *a = "com42FA"; 如何检查字符串包含数字、十六进制或数字但检查从字符 4 开始到字符 7.
一个简单的解决方案是正则表达式
模式在字符集0-9
、A-F
和a-f
中搜索4个字符。
显式范围 NSMakeRange(3, 4)
搜索字符 4 - 7(location
参数从零开始)。
NSString *a = @"com42FA";
NSString *pattern = @"[0-9A-Fa-f]{4}";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSRange range = [regex rangeOfFirstMatchInString:a options:0 range:NSMakeRange(3, 4)];
BOOL hexNumberFound = range.location != NSNotFound;
NSLog(@"%d", hexNumberFound);
您可以使用 NSScanner
,它在几乎所有情况下都比正则表达式更快:
NSString *initialString = @"com42FA";
NSScanner *scanner = [NSScanner scannerWithString:initialString];
// Setup the scanner. Depends on your needs
scanner.caseSensitive = NO;
scanner.charactersToBeSkipped = nil;
// Specify the location to start scan from
scanner.scanLocation = 3;
// Actual scanning, note that I'm checking that the scanner at the end
// to understand whether it scanned up to the end of the string
unsigned long long scannedNumber = 0;
BOOL success = [scanner scanHexLongLong:&scannedNumber] && scanner.isAtEnd;
if (success) {
NSLog(@"%llu", scannedNumber); // 17146
<...>
}
作为第三种选择(无论如何在撰写本文时!)一个简单的循环将处理这个:
NSString *testString = @"com42FA";
NSCharacterSet *hexDigits = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
NSUInteger pos = 3;
BOOL isValid = YES;
while (pos <= 6 && isValid) isValid = [hexDigits characterIsMember:[testString characterAtIndex:pos++]];
只要发现无效数字就循环检查并停止。
附录
由于其他答案提出了性能问题,并不是说对于这么小的任务它可能是个问题,我提供以下更快的变体:
NSString *testString = @"com42FA";
NSUInteger pos = 3;
BOOL isValid = YES;
while (pos <= 6 && isValid) isValid = isxdigit([testString characterAtIndex:pos++]);
这使用标准 C 库函数 isxdigit()
,避免了 NSCharacterSet
创建和方法调用的需要。 (这可能也不是最快的选择,但在这一点之后可读性可能会受到影响。)