Removing new line characters from NSString

Solution 1:

Split the string into components and join them by space:

NSString *newString = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];

Solution 2:


Splitting the string into components and rejoining them is a very long-winded way to do this. I too use the same method Paul mentioned. You can replace any string occurrences. Further to what Paul said you can replace new line characters with spaces like this:

myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];

Solution 3:

I'm using

[...]
myString = [myString stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"];
[...]

/Paul