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.
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.
Here is a Swift 3 working version, for NSRange, Range, String and NSString! Enjoy :)
Your solution did not work for me, I added a condition in the loop to increment numberOfChar only if mainScanner has reached the end of the string :
Note that this is a quick fix, I don't have time to make an elegant solution...
Try this category on NSString:
This one is approximately 5 times faster than the
componentsSeparatedByString:
approach.