detect number of times a character is in a string.
I have a string with alot of text..
I want to detect how many times @"hello" is in the string... I know how to detect if it is or isn't but how do I detect the number of times it appears in the string?
__________________
APPS4LIFE
search for me in the app store.
I have a string with alot of text..
I want to detect how many times @"hello" is in the string... I know how to detect if it is or isn't but how do I detect the number of times it appears in the string?
I would suggest writing a method countOccurrencesOfString:inString:
You could write it by calling rangeOfStringptions:range: repeatedly. You'd set up an NSRange of the entire string to start with. Then if the first call found any occurrences, you'd increment a count, calculate a new NSRange that would start at the next character after the first occurrence, and keep searching while more occurrences are found.
Note that the code above is completely untested. I did not even check to see if it compiles. There may be syntax errors, logic errors, etc. Testing and debugging it is an "exercise for the reader."
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Thankyou for posting your code too though, it is neat to see other ways to do it... you build a whole counting engine.. very nice
Thanks again!
As predicted, my code had a few errors in it.
The regex-based version is more powerful. It will handle cases like searching for "the" when the string contains "their". My code would count "their" as containing "the". The regex version you posted will only match whole words.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
regex contains "options" (e.g. case-sensitive, alphanumeric only, etc.) I'm sure there is one for that too... I hope /: hhahah Because mine may need to involve that soon. Haha
__________________
APPS4LIFE
search for me in the app store.
regex contains "options" (e.g. case-sensitive, alphanumeric only, etc.) I'm sure there is one for that too... I hope /: hhahah Because mine may need to involve that soon. Haha
Both the code I posted and the regex string counting can do case sensitive/case insensitive, ignore diacritical marks, etc.
regex-based searching has lots more options however. You can set up a regular expression so a string only matches if it is a whole word. That' what the example you posted does. By enclosing your word between "/b" entries ("/bhello/b") it makes sure the word hello has a word break on either side.
You can also create regular expressions that will take characters from a list of characters. You could match a string that had an a, b, or c as the first character, a 1, 2, 3 as the second character, and an x, y, or z as the last character, for example.
All that would be a pain in the butt to do manually with code.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.