使用NSScanner阅读新行

时间:2013-10-07 21:43:44

标签: iphone nsstring nsscanner

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

    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

任何想法如何使它工作?

1 个答案:

答案 0 :(得分:1)

默认情况下,

NSScanner会跳过空白区域。 要修复,请添加:

[scanner setCharactersToBeSkipped:nil];