将空白序列折叠成单个字符并修剪字符串

考虑下面的例子:

"    Hello      this  is a   long       string!   "

我想把它转换成:

"Hello this is a long string!"
64021 次浏览

下面是 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;
}

这个应该可以..。

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:@" "];

另一种解决方案: 给自己一个 OgreKit (Cocoa 正则表达式库)的副本。

  • OgreKit (Japanese webpage -- 代码为英文)
  • OgreKit (Google 自动翻译) :

整个函数就是:

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

简短而甜蜜。

如果您想要最快的解决方案,那么使用 NSScanner精心构造的一系列指令可能是最好的,但只有在您计划处理大量(许多兆字节)文本块时才有必要这样做。

Regex 的另一个选项是 RegexKitLite,它非常容易嵌入到 iPhone 项目中:

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

事实上,有一个非常简单的解决办法:

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

(来源)

OS X 10.7 + 和 iOS 3.2 +

使用 hhossli 提供的原生 Regexp 解决方案 regexp 解决方案

Otherwise

要么使用您喜欢的 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:@" "];

一个简单的解决办法:

NSString *whitespaceString = @" String with whitespaces ";


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

使用 regex,但不需要任何外部框架:

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


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

according from @Mathieu Godart is best answer, but some line is missing , all answers just reduce space between words , but when if have tabs or have tab in place space , like this: " this is text \t , and\tTab between , so on " 在三行代码中,我们将: 我们想要减少空白的字符串

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"

不替换 tab 的结果将是:

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

试试这个

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


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

根据需求,以下两个正则表达式可以工作

  1. @ “ +”用于匹配空格和制表符
  2. 用于匹配空格、制表符和换行符

然后应用 nsstring 的实例方法 stringByReplacingOccurrencesOfString:withString:options:range:将它们替换为一个空格。

例如:。

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

注意: 对于 iOS 5.x 及以上版本的上述功能,我没有使用“ RegexKitLite”库。

这个解决方案可以帮助你修剪前面和后面的空格以及多个空格。

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]];

Logging final gives

"Hello this is a long string!"

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

  • 只替换空格: [ ]+
  • 替换空格和制表符: [ \\t]+
  • 替换空格、制表符和换行符: \\s+

Performance rundown

扩展的易用性、性能、代码行数和创建的对象数使得这个解决方案非常合适。

You can also use a simple while argument. There is no RegEx magic in there, so maybe it is easier to understand and alter in the future:

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