gpt4 book ai didi

ios - 删除字符串中单词之间的空格,但一个

转载 作者:行者123 更新时间:2023-11-28 18:18:10 24 4
gpt4 key购买 nike

这里是片段将解释它。基本上,我想删除两个单词之间的所有空格,除了一个。有没有更简单的股票方法来做到这一点?

NSString *originalString = @"one two  three    four        five";
NSArray *stringArray = [originalString componentsSeparatedByString:@" "];
NSPredicate *whiteSpacePredicate = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *stringArray2 = [stringArray filteredArrayUsingPredicate:whiteSpacePredicate];
NSString *newString = @"";
for (NSString *string in stringArray2)
{
newString = [[newString stringByAppendingString:string] stringByAppendingString:@" "];
}
NSLog(@"originalString: %@", originalString);
NSLog(@"newString: %@", newString);

最佳答案

你可以这样做

NSString *newString= [originalString stringByReplacingOccurrencesOfString: @"[ \t]+"
withString: @" "
options: NSRegularExpressionSearch
range: NSMakeRange(0, originalString.length)];

这将从原始字符串中删除所有多余的空格和制表符。

关于ios - 删除字符串中单词之间的空格,但一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28527781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com