iPhone-SDK:从段落字符串中删除空格? [英] iPhone-SDK:Remove white spaces from a paragraph string?
问题描述
我在一个字符串中有两个段落。但是内容在段落字符串之间有很多空格。我想从这个大段落字符串中删除空格。我尝试使用
NSString * outPutStr = [outPutStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
但是它不会删除段落字符串中的空格。它什么都不做。
I have two paragraph in a string. But the contents are having many spaces in between the paragraph string. I want to remove the spaces from this big paragraph string. I tried using NSString *outPutStr = [outPutStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; But it doesn't remove empty spaces that are there in the paragraph string. It does nothing.
有人可以指导我提供样品吗?
Can someone please guide me with sample?
谢谢。
Clave /
推荐答案
如果你使用了很棒的框架 RegexKitLite ,非常容易嵌入到iPhone项目中:
If you use the awesome framework RegexKitLite, which is very easy to embed in an iPhone project:
NSString *cleanString = [yourStringWithWhiteSpaces stringByReplacingOccurrencesOfRegex:@" +" withString:@" "];
这表示只要有一个空格,任何时候你发现任何超过1个字符的空格。我不知道你的来源,但你可以通过使用简单的
This says replace anytime you find any space that is longer than 1 character with a single space. I dont know your source, but you could do the same for new lines and carriage returns by using the simple
NSString *cleanerString = [cleanString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *theCleanestString = [cleanerString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
注意*有更简洁的方法在代码中执行此操作,将它们合并为一步,但这是一种分解方法,因此您可以轻松了解正在进行的操作并有助于调试。
Note* there are cleaner ways to do this in the code, combining them into 1 step, but this is a way to break it down so you can easily understand what is going on and helpful for debugging.
这篇关于iPhone-SDK:从段落字符串中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!