0
是否可以看到一個字符串以數字結尾,而該數字的長度未知?檢查NSString的最後一個字符是否爲數字
- 「字符串1」 - > 1
- 「字符串4356」 - > 4356
- 「字符串」 - >零
如果是這樣,我怎麼能確定這個數字?
是否可以看到一個字符串以數字結尾,而該數字的長度未知?檢查NSString的最後一個字符是否爲數字
如果是這樣,我怎麼能確定這個數字?
要測試一個字符串結尾的數字,你可以使用一個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。
感謝您的回覆。我認爲您的第二個片段中有兩個拼寫錯誤。範圍應該是digitRange,==應該是!=正確的?另外,第二個片段找到值爲12的2。我想找到所有正數,而不是關於它們的密碼量。這也可能與您的第二個片段,或者我應該去的正則表達式庫? – 2010-04-03 11:59:29
@邁克爾:是的。本來,我有「字符串不以數字結尾」分支,顯然,我忽略了對測試的反轉。我會用一個片段更新我的答案,一旦我休息一下,就會提取整個數字。 – outis 2010-04-03 13:14:42
感謝您爲我檢查出來。 – 2010-04-03 14:04:16