Iphone iterate over substring occurrences of a NSString

iphone, nsstring

Solution

How about

// find first occurrence of search string in source string
NSRange range = [sourceString rangeOfString:@"searchString"];
while(range.location != NSNotFound)
{
    // build a new string with your changed values

    range = [sourceString rangeOfString:@"searchString" options:0 range:NSMakeRange(range.location + 1, [sourceString length] - range.location - 1)];
}

Or just

[sourceString stringByReplacingOccurrencesOfString:searchString withString:targetString];

if you want to change the searchString to the same value everywhere in the source string.

Problem

I would like to find all occurrences of a substring in a NSString, and iterate one by one to do some changes to that NSString. How should I do it?

Original source