是否可以看到一个字符串以一个长度未知的数字结尾?
如果是,我该如何确定该号码?
答案 0 :(得分:11)
要测试字符串是否以数字结尾,您可以使用NSPredicate,例如:
NSPredicate endsNumerically = [NSPredicate predicateWithFormat:@"SELF matches %@", @"\\d+$"];
[endsNumerically evaluateWithObject:string]; // returns TRUE if predicate succeeds
NSScanner
有时可用于从字符串中提取内容,但不会向后扫描。您可以定义一个Gnirts(反向字符串)类并将其与NSScanner一起使用,但这可能比它的价值更麻烦。
NSString的rangeOfCharacterFromSet:options:
,我希望使用它,只查找单个字符(就像strchr
and strrchr
一样,如果你熟悉C),但我们可以自己滚动返回一个一组中的连续字符范围(有点像strspn
)和NSString
上的category。当我们处于它的时候,让我们包括返回子串而不是范围的方法。
RangeOfCharacters.h:
@interface NSString (RangeOfCharacters)
/* note "Characters" is plural in the methods. It has poor readability, hard to
* distinguish from the rangeOfCharacterFromSet: methods, but it's standard Apple
* convention.
*/
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet;
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask;
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask range:(NSRange)range;
// like the above, but return a string rather than a range
-(NSString*)substringFromSet:(NSCharacterSet*)aSet;
-(NSString*)substringFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask;
-(NSString*)substringFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask range:(NSRange)range;
@end
RangeOfCharacters.m:
@implementation NSString (RangeOfCharacters)
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet {
return [self rangeOfCharactersFromSet:aSet options:0];
}
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask {
NSRange range = {0,[self length]};
return [self rangeOfCharactersFromSet:aSet options:mask range:range];
}
-(NSRange)rangeOfCharactersFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask range:(NSRange)range {
NSInteger start, curr, end, step=1;
if (mask & NSBackwardsSearch) {
step = -1;
start = range.location + range.length - 1;
end = range.location-1;
} else {
start = range.location;
end = start + range.length;
}
if (!(mask & NSAnchoredSearch)) {
// find first character in set
for (;start != end; start += step) {
if ([aSet characterIsMember:[self characterAtIndex:start]]) {
#ifdef NOGOTO
break;
#else
// Yeah, a goto. If you don't like them, define NOGOTO.
// Method will work the same, it will just make unneeded
// test whether character at start is in aSet
goto FoundMember;
#endif
}
}
#ifndef NOGOTO
goto NoSuchMember;
#endif
}
if (![aSet characterIsMember:[self characterAtIndex:start]]) {
NoSuchMember:
// no characters found within given range
range.location = NSNotFound;
range.length = 0;
return range;
}
FoundMember:
for (curr = start; curr != end; curr += step) {
if (![aSet characterIsMember:[self characterAtIndex:curr]]) {
break;
}
}
if (curr < start) {
// search was backwards
range.location = curr+1;
range.length = start - curr;
} else {
range.location = start;
range.length = curr - start;
}
return range;
}
-(NSString*)substringFromSet:(NSCharacterSet*)aSet {
return [self substringFromSet:aSet options:0];
}
-(NSString*)substringFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask {
NSRange range = {0,[self length]};
return [self substringFromSet:aSet options:mask range:range];
}
-(NSString*)substringFromSet:(NSCharacterSet*)aSet options:(NSStringCompareOptions)mask range:(NSRange)range {
NSRange range = [self rangeOfCharactersFromSet:aSet options:mask range:range];
if (NSNotFound == range.location) {
return nil;
}
return [self substringWithRange:range];
}
@end
使用新类别检查字符串是否以数字结尾或提取数字:
NSString* number = [string substringFromSet:[NSCharacterSet decimalDigitCharacterSet]
options:NSBackwardsSearch|NSAnchoredSearch];
if (number != nil) {
return [number intValue];
} else {
// string doesn't end with a number.
}
最后,您可以使用第三方正则表达式库,例如RegexKit或RegexkitLite。
答案 1 :(得分:0)
尽管看起来应该正确,但我无法使上面的NSPredicate
代码正常工作。相反,我用
if ([string rangeOfString:@"\\d+$" options:NSRegularExpressionSearch].location != NSNotFound) {
// string ends with a number
}
this answer的提示。