请考虑以下示例:
" Hello this is a long string! "
我想将其转换为:
"Hello this is a long string!"
Georg Schöll.. 124
OS X 10.7+和iOS 3.2+
使用hfossli提供的本机regexp解决方案.
除此以外使用您喜欢的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:@" "];
我很好奇将性能与正则表达式的替换进行比较,并使用修剪来移除末端.一方面,你有一个正则表达式来处理.另一方面,你有一个谓词.要么需要对各个表达式进行内部处理. (4认同)
很好的回答,赞成这样,但我挑战你对"简单"的定义.真诚的,前Python家伙现在在ObjC-land ;-) (2认同)
你让我笑了起来,"如果有一个简单的解决方案,请不要使用复杂的解决方案".所以最容易的是[toBeTrimmed stringByReplacingOccurrencesOfString:@""withString:@""]没有?我仍然赞同你的答案,但这绝对是最简单的 (2认同)
@MárioCarvalho问题是如何删除*多余*空格,而不是全部. (2认同)
hfossli.. 52
正则表达式和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+
性能破坏
此解决方案:7.6秒
拆分,过滤,加入(GeorgSchölly):13.7秒
易于扩展,性能,代码行数和创建的对象数使该解决方案合适.
使用hfossli提供的本机regexp解决方案.
除此以外使用您喜欢的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:@" "];
正则表达式和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+
性能破坏
此解决方案:7.6秒
拆分,过滤,加入(GeorgSchölly):13.7秒
易于扩展,性能,代码行数和创建的对象数使该解决方案合适.
实际上,有一个非常简单的解决方案:
NSString *string = @" spaces in front and at the end "; NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; NSLog(@"%@", trimmedString)
(来源)
使用正则表达式,但不需要任何外部框架:
NSString *theString = @" Hello this is a long string! "; theString = [theString stringByReplacingOccurrencesOfString:@" +" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, theString.length)];
一线解决方案:
NSString *whitespaceString = @" String with whitespaces "; NSString *trimmedString = [whitespaceString stringByReplacingOccurrencesOfString:@" " withString:@""];
这应该做到......
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:@" "];