Tuesday, March 16, 2010

Count No of occurrences of a String or a character

Hi Friends,

I was in a search of a function that would give the number of occurrences of a character or a string in another string and finally I landed in the following solution. If you have any other way to achieve it, please share it.

+ (NSInteger)occurrencesOf:(NSString *)str InString:(NSString *)mainString
{

return [[mainString componentsSeparatedByString:str] count];

}

Thank you.

No comments:

Post a Comment