I have an NSString
or NSMutableString
and would like to get the number of occurrences of a particular character.
I need to do this for quite a few characters -- uppercase English characters in this case -- so it would be nice for it to be quick.
You can do this in one line. For example, this counts the number of spaces:
NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:@" "] count] - 1;