0

我有一个 NSMutableString* (theBigString),其中包含从在线文件加载的文本。我正在将 theBigString 写入 iPad 上的本地文件,然后将其作为附件通过电子邮件发送。

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

尽管我尽最大努力使用以下代码行,但当我打开电子邮件附件时,仍然存在多行文本,而不是我预期的一长行文本(并且需要我的应用程序工作)。

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

如果我打开文本文件并查看格式,它仍然会显示几个新的段落符号。

除了“\n”或“\r”之外,是否还有另一个换行符类型字符我没有用上面的代码剥离?

4

1 回答 1

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:@""];
于 2014-07-27T19:24:43.913 回答