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

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

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

How can I replacing string with any numbers ?

count '0' , count '1' , count '2'

count 'any numbers'

I already tried the loop:

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

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

2 Answers2

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

should do the trick. "\d+" is a regular expression pattern that matches one or more digits.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Little typo though, it should be `@"count '\\d',"` and it will leave white spaces. – Desdenova Feb 21 '14 at 12:51
  • 1
    @Desdenova: `"\\d+"` is for *one or more* digits, so that would also match `"count '10'"` or `"count '9999'"`. - I don't know which white space you mean. I have just generalized the pattern `"count '0','"` from the question. It can be improved if OP shows a complete example of the input and the expected output. – Martin R Feb 21 '14 at 13:00
  • by typo I meant the extra `'` in the end. You were right about the `\d+`, tried both with the plus and without, seems like both are working. The spaces after and before the coma marks, log a length and you'll see. – Desdenova Feb 21 '14 at 13:06
  • 1
    @Desdenova: The trailing `'` is already present in the original patterns shown in the question. I cannot *guess* if that was intended or not. – Martin R Feb 21 '14 at 13:17
1

This will help you:

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]]);
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
  • Yes this is more like it, only problem is you are leaving a lot of white spaces which needs cleaning as well. Converted my down vote to up vote though. – Desdenova Feb 21 '14 at 12:20
  • We can use `[NSString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]` for that. – Sujith Thankachan Feb 21 '14 at 12:22