user714171
user714171

Reputation: 435

Read new lines with NSScanner

I'm trying to read the characters between words in a string.

    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);
}

the output is:

xx
xx
xx
xx

I would expect it to be:

x x

x

x

x x

x 

x

Any ideas how to make it work?

Upvotes: 0

Views: 227

Answers (1)

user714171
user714171

Reputation: 435

NSScanner skips white space by default. To fix, add:

[scanner setCharactersToBeSkipped:nil];

Upvotes: 1

Related Questions