0

I have an NSMutableString* (theBigString) that contains text loaded from an online file. I am writing theBigString to a local file on the iPad and then emailing it as an attachment.

[theBigString appendString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];

Despite my best efforts using the following lines of code, when I open the email attachment there still exists multiple lines of text instead of one long line of text which is what I expected (and need for my app to work).

[theBigString stringByReplacingOccurrencesOfString:@"\\s+" withString:@" "];
[theBigString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
[theBigString stringByReplacingOccurrencesOfString:@"\r" withString:@" "];
[theBigString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];

If I open the text file and view the formatting it still shows a couple of new paragraph symbols.

Is there another newline type character besides "\n" or "\r" that I am not stripping with the above code?

Scooter
  • 4,068
  • 4
  • 32
  • 47

1 Answers1

1

NSCharacterSet defines [NSCharacterSet newLineCharacterSet] as (U+000A–U+000D, U+0085)

If you don't care too much about efficiency, you could also just separate the string into an array at the newline character locations and combine it again with empty strings.

NSArray* stringComponents = [theBigString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
theBigString = [stringComponents componentsJoinedByString:@""];
Nyth
  • 582
  • 5
  • 16
  • This worked like a champ! I have no idea why what I was doing didn't. As an aside I used @" " in componentsJoinedByString to put the spaces back. – Scooter Jul 27 '14 at 21:11