Fah*_*kar 1 replace objective-c ios
早些时候,我有字符串 1,2,3,,5,6,7
要替换字符串,我使用了stringByReplacingOccurrencesOfString:@",," withString:@","
,输出为1,2,3,5,6,7
现在我有如下字符串.
1,2,3,,,6,7
要替换字符串,我使用了stringByReplacingOccurrencesOfString:@",," withString:@","
,输出为1,2,3,,6,7
有没有办法我可以用单个逗号替换所有双逗号.
我知道我可以使用for循环或while循环,但我想检查有没有其他方法?
for (int j=1;j<=100;j++) {
stringByReplacingOccurrencesOfString:@",," withString:@","]]
}
Run Code Online (Sandbox Code Playgroud)
NSString *string = @"1,2,3,,,6,7";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@",{2,}" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@","];
NSLog(@"%@", modifiedString);
Run Code Online (Sandbox Code Playgroud)
这将匹配,
字符串中存在的任何数量.这是未来的证明:)
归档时间: |
|
查看次数: |
108 次 |
最近记录: |