过滤出双倍空格 [英] Filter out double space

查看:44
本文介绍了过滤出双倍空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码过滤掉换行符等,并用空格替换它们.然后,我使用另一行占据该双倍空格,并仅用一个空格替换它,但看起来仍然像是有一个双倍空格.

I am using the following code to filter out line breaks, etc., and replace them with spaces. Then, I am using another line to take that double space and replace it with only one space, but it still looks like there is a double space.

我在做什么错了?

NSString *body = [message bodyPreferringPlainText:&isPlain];
body = [body stringByReplacingOccurrencesOfString:@"\r" withString:@"\n"];
body = [body stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
body = [body stringByReplacingOccurrencesOfString:@"  " withString:@" "];

http://i.stack.imgur.com/yNX6O.png

编辑:我想我已经找到了问题,只有在消息的格式如下时才会发生这种情况:

EDIT: I think I have found my problem, this only happens when the message is formatted like so:

Hello,

How are you doing?

Thanks!

但不是这样的时候:

Hello,
How are you doing?
Thanks!

有什么想法吗?

推荐答案

在GoZoner的帮助下,我发现这是我需要的代码:

I have figured out, with the help of GoZoner, that this is the code I needed:

NSString *body = [message bodyPreferringPlainText:&isPlain];
    body = [[body componentsSeparatedByCharactersInSet:
             [NSCharacterSet whitespaceAndNewlineCharacterSet]]
            componentsJoinedByString:@" "];
    body = [body stringByReplacingOccurrencesOfString:@"  " withString:@" "];

这篇关于过滤出双倍空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆