0

我正在尝试读取字符串中单词之间的字符。

    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

任何想法如何使它工作?

4

1 回答 1

1

NSScanner默认跳过空白。要修复,请添加:

[scanner setCharactersToBeSkipped:nil];
于 2013-10-08T21:17:16.627 回答