Reputation: 215
Programmatically I can see how many times a character exists in any given string:
letterOccurences= [[tempWordStr componentsSeparatedByString:chosenCharacter] count];
So for the word Mississippi, if the chosen character was 'i' this function would return 4.
But what I would like to know is where the character appears in the string. In the Mississippi case, this would be at 1,4,7,10.
Upvotes: 2
Views: 723
Reputation: 89152
Using your trick of splitting the string, instead of just getting the count of elements in the returned array, get the length of each element in the array. Loop and accumulate the lengths and add 1 for the missing "i".
Upvotes: 1
Reputation: 31294
You can use the NSString
methodrangeOfString:options:range:
- this finds and returns the range of a given string (ie, @"i"
) within a given range. You simply need to adjust the given range of the receiver until you find no more instances of the substring.
Upvotes: 2