GoRoS
GoRoS

Reputation: 5375

Read text file's lines

I get a remote text file with a specific number of lines. Every line has the same format, and at the end of each one it has a 0x0D(carriage return) and 0x0A(new line).

As a consecuence if I run the following code:

NSError *error = nil;
NSString *words = [[NSString alloc] initWithContentsOfFile:path
                              encoding:NSUTF8StringEncoding error:&error];
NSArray* lines = [words componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];   

for(NSString *str in lines){
        NSLog(@"%@",str);
}

I get a blank line between every text line because of those line ending characters. How would you customize my reading and read those lines without those empty ones?

Upvotes: 2

Views: 1547

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90681

Take a look at -[NSString enumerateLinesUsingBlock:].

Upvotes: 3

thundersteele
thundersteele

Reputation: 673

You can try the following:

NSArray* lines = [words componentsSeparatedByString:@"\r\n"]];

Upvotes: 1

Related Questions