stringByReplacingOccurrencesOfString循环?

时间:2014-02-21 11:47:19

标签: objective-c nsstring

NSString *qwer2 = [qwer1 stringByReplacingOccurrencesOfString:@"count '0','" withString:@""];

NSString *qwer4 = [qwer2 stringByReplacingOccurrencesOfString:@"count '1','" withString:@""];

NSString *qwer = [qwer4 stringByReplacingOccurrencesOfString:@"count '2','" withString:@""];

如何用任何数字替换字符串?

  

计数'0',计数'1',计数'2'

     

计算'任何数字'

我已经尝试了循环:

for (int i =0; i<100; i++) 

NSString * qwer = [qwer4 stringByReplacingOccurrencesOfString:@“count'i','”withString:@“”];

2 个答案:

答案 0 :(得分:4)

NSString *replaced = [string stringByReplacingOccurrencesOfString:@"count '\\d+','"
                           withString:@""
                              options:NSRegularExpressionSearch
                                range:NSMakeRange(0, string.length)];

应该做的伎俩。 &#34; \ d +&#34;是一个匹配一个或多个数字的正则表达式模式。

答案 1 :(得分:1)

这将对您有所帮助:

NSString *qwer1 = @"My string count '0', count '1', count '2', count '3', count '4',";
for (int i =0; i<5; i++) {
    NSString *count = [NSString stringWithFormat:@"count '%i',", i];
    qwer1 = [qwer1 stringByReplacingOccurrencesOfString:count withString:@""];
}
NSLog(@"%@", [qwer1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);