我正在尝试读取字符串中单词之间的字符。
NSCharacterSet* whiteSpace = [NSCharacterSet characterSetWithCharactersInString:@" \n\r\t"];
NSScanner* testScanner = [NSScanner scannerWithString:@"space newline\n space space newline\r end"];
while([testScanner isAtEnd] == NO) {
NSString* spaceBetweenWords = @"";
[testScanner scanUpToCharactersFromSet:whiteSpace intoString:NULL];
[testScanner scanCharactersFromSet:whiteSpace intoString:&spaceBetweenWords];
NSLog(@"x%@x", spaceBetweenWords);
}
输出是:
xx
xx
xx
xx
我希望它是:
x x
x
x
x x
x
x
任何想法如何使它工作?
答案 0 :(得分:1)
NSScanner
会跳过空白区域。
要修复,请添加:
[scanner setCharactersToBeSkipped:nil];