Reputation: 3061
Is there any way of counting how many times one string occurs in another. Eg. how many times does "/" appear in "bla/hsi/sgg/shrgsvs/"= 4.
Upvotes: 0
Views: 859
Reputation: 9126
You could do:
NSArray *a = [myString componentsSeparatedByString:@"/"];
int i = [a count] - 1;
But that's really quick and dirty. Someone else might come up with a better answer shortly.
EDIT:
Now that I think about it, this might work too:
NSUInteger count = 0;
NSUInteger length = [str length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [str rangeOfString: @"/" options:0 range:searchRange);
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
count++;
}
}
Although I still think there's gotta be a better way...
Upvotes: 4