如何清理 NSString 中的空格
我想将 NSString(Objective-C/iPhone dev)中两个单词之间的空格数量减少到只有一个。你知道有什么方法可以做到这一点吗?
示例:
清洁前:“嗨, 我的 名字 是 汤姆。”
清洁后:“嗨,我叫汤姆。”
谢谢
I would like to reduce the number of spaces between two words in a NSString (Objective-C/iPhone dev) to only one. Do you know some method that could do that ?
Exemple :
Before cleaning : "Hi, my name is Tom."
After cleaning : "Hi, my name is Tom."
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
[mystring ComponentsSeparatedByString:@" "]
获取由单个空格分隔的所有子字符串的NSArray
。然后,重新组合数组中的非空字符串,得到最终的字符串。Use
[mystring componentsSeparatedByString:@" "]
to get aNSArray
of all substrings separated by a single space. Then, recombine the non-empty strings in the array to get the final string.看看 http://regexkit.sourceforge.net/,它有
NSString 扩展,您可以使用它进行基于正则表达式的字符串替换。
Take a look at http://regexkit.sourceforge.net/, it has
NSString
extensions with which you could do regex-based string replace.使用 RegexKit,添加到 RSC 的答案
using RegexKit, adding to RSC's answer