1
我一直在試圖過濾用下面的代碼之後的字符串:濾波器NSScanner一個String和去除空白數字字符
//the String with the original text
NSString *unfilteredString = @"(A1)";
//initialize a string that will hold the result
NSMutableString *resultString = [NSMutableString stringWithCapacity:unfilteredString.length];
NSScanner *scanner = [NSScanner scannerWithString:unfilteredString];
NSCharacterSet *allowedChars = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {
[resultString appendString:buffer];
}
else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog (@"Result: %@", resultString);
這給我結果:
結果: (A)
正如您所看到的,它不僅刪除了數字1,而且還刪除了尾部空格。
有什麼提示嗎?
您希望接收的結果是什麼? – jonkroll 2012-01-06 16:14:15
我期望(A) – 2012-01-06 19:40:34