将白色空间的序列折叠成单个字符和修剪字符串

时间:2009-04-16 21:59:43

标签: objective-c ios nsstring

考虑以下示例:

"    Hello      this  is a   long       string!   "

我想将其转换为:

"Hello this is a long string!"

13 个答案:

答案 0 :(得分:124)

OS X 10.7+和iOS 3.2 +

使用hfossli提供的原生regexp solution

,否则

使用您喜欢的regexp库或使用以下Cocoa原生解决方案:

NSString *theString = @"    Hello      this  is a   long       string!   ";

NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];

NSArray *parts = [theString componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
theString = [filteredArray componentsJoinedByString:@" "];

答案 1 :(得分:52)

Regex和NSCharacterSet随时为您提供帮助。此解决方案修剪前导和尾随空白以及多个空格。

NSString *original = @"    Hello      this  is a   long       string!   ";

NSString *squashed = [original stringByReplacingOccurrencesOfString:@"[ ]+"
                                                         withString:@" "
                                                            options:NSRegularExpressionSearch
                                                              range:NSMakeRange(0, original.length)];

NSString *final = [squashed stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

记录final给出

"Hello this is a long string!"

可能的替代正则表达式模式:

  • 仅替换空格:[ ]+
  • 替换空格和标签:[ \\t]+
  • 替换空格,制表符和换行符:\\s+

Performance rundown

易于扩展,性能,代码行数和创建的对象数使该解决方案合适。

答案 2 :(得分:40)

实际上,有一个非常简单的解决方案:

NSString *string = @" spaces in front and at the end ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"%@", trimmedString)

Source

答案 3 :(得分:13)

使用正则表达式,但不需要任何外部框架:

NSString *theString = @"    Hello      this  is a   long       string!   ";

theString = [theString stringByReplacingOccurrencesOfString:@" +" withString:@" "
                       options:NSRegularExpressionSearch
                       range:NSMakeRange(0, theString.length)];

答案 4 :(得分:9)

一线解决方案:

NSString *whitespaceString = @" String with whitespaces ";

NSString *trimmedString = [whitespaceString
        stringByReplacingOccurrencesOfString:@" " withString:@""];

答案 5 :(得分:6)

应该这样做......

NSString *s = @"this is    a  string    with lots  of     white space";
NSArray *comps = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSMutableArray *words = [NSMutableArray array];
for(NSString *comp in comps) {
  if([comp length] > 1)) {
    [words addObject:comp];
  }
}

NSString *result = [words componentsJoinedByString:@" "];

答案 6 :(得分:4)

正则表达式的另一个选项是RegexKitLite,它非常容易嵌入到iPhone项目中:

[theString stringByReplacingOccurencesOfRegex:@" +" withString:@" "];

答案 7 :(得分:3)

以下是NSString扩展程序的摘录,其中"self"NSString个实例。它可以通过将[NSCharacterSet whitespaceAndNewlineCharacterSet]' '传递给两个参数来将连续的空格折叠到一个空格中。

- (NSString *) stringCollapsingCharacterSet: (NSCharacterSet *) characterSet toCharacter: (unichar) ch {
int fullLength = [self length];
int length = 0;
unichar *newString = malloc(sizeof(unichar) * (fullLength + 1));

BOOL isInCharset = NO;
for (int i = 0; i < fullLength; i++) {
    unichar thisChar = [self characterAtIndex: i];

    if ([characterSet characterIsMember: thisChar]) {
        isInCharset = YES;
    }
    else {
        if (isInCharset) {
            newString[length++] = ch;
        }

        newString[length++] = thisChar;
        isInCharset = NO;
    }
}

newString[length] = '\0';

NSString *result = [NSString stringWithCharacters: newString length: length];

free(newString);

return result;
}

答案 8 :(得分:3)

试试这个

NSString *theString = @"    Hello      this  is a   long       string!   ";

while ([theString rangeOfString:@"  "].location != NSNotFound) {
    theString = [theString stringByReplacingOccurrencesOfString:@"  " withString:@" "];
}

答案 9 :(得分:-1)

替代解决方案:给自己一份OgreKit(Cocoa正则表达式库)。

  • OgreKit(日文网页 - 代码是英文)
  • OgreKit(Google 自动转换):

整个功能是:

NSString *theStringTrimmed =
   [theString stringByTrimmingCharactersInSet:
        [NSCharacterSet whitespaceAndNewlineCharacterSet]];
OGRegularExpression  *regex =
    [OGRegularExpression regularExpressionWithString:@"\s+"];
return [regex replaceAllMatchesInString:theStringTrimmed withString:@" "]);

简短又甜蜜。

如果您正在使用最快的解决方案,那么使用NSScanner精心构建的一系列指令可能效果最好,但只有在您计划处理大量(多兆字节)文本块时才有必要。 / p>

答案 10 :(得分:-1)

来自@Mathieu Godart是最好的答案,但有些线条缺失,所有答案只是减少了单词之间的空间,但是如果有标签或者有空格的标签,就像这样: “这是文字\ t,\ TTab之间,等等” 在三行代码中我们将: 我们想要的字符串减少空格

NSString * str_aLine = @"    this is text \t , and\tTab between      , so on    ";
// replace tabs to space
str_aLine = [str_aLine stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
// reduce spaces to one space
str_aLine = [str_aLine stringByReplacingOccurrencesOfString:@" +" withString:@" "
                                                    options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, str_aLine.length)];
// trim begin and end from white spaces
str_aLine = [str_aLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

结果是

"this is text , and Tab between , so on"

没有替换标签,结果将是:

"this is text    , and  Tab between , so on"

答案 11 :(得分:-1)

根据要求,以下两个正则表达式将起作用

  1. @“+”用于匹配空格和制表符
  2. @“\\ s {2,}”用于匹配空格,制表符和换行符
  3. 然后应用nsstring的实例方法stringByReplacingOccurrencesOfString:withString:options:range:将它们替换为一个空格。

    e.g。

    [string stringByReplacingOccurrencesOfString:regex withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])];
    

    注意:我没有将'RegexKitLite'库用于iOS 5.x及更高版本的上述功能。

答案 12 :(得分:-1)

您也可以使用简单的while参数。那里没有RegEx魔法,所以将来可能更容易理解和改变:

while([yourNSStringObject replaceOccurrencesOfString:@"  "
                         withString:@" "
                         options:0
                         range:NSMakeRange(0, [yourNSStringObject length])] > 0);