views:

2495

answers:

5

I have an NSString and would like to get the number of occurrences of a particular character.

I need to do this for quite a few characters, so it would be nice for it to be quick.

In case it makes any difference, it's actually an NSMutableString, and the characters will all be uppercase English letters.

A: 

I would probably use

NSString rangeOfCharacterFromSet:

or

rangeOfCharacterFromSet:options:range::

where the set is the set of characters you're searching for. It returns with the location of first character matching the set. Keep array or dictionary and increment the count for character, then repeat.

stefanB
If I'm understanding the Documentation correctly, this would give the range of any of the characters in the set. But I need the count of *each* character.
Elliot
my idea is to keep dictionary of char -> count pairs and then get the char at given index and increment it's count in dictionary ... or you could just iterate over the string and check if each character is in your set, if it is then increment it's count
stefanB
+1  A: 

This will return the number of characters replaced in a mutable string.

[string replaceOccurrencesOfString:@"A" withString:@"B" options:NSLiteralSearch range:NSMakeRange(0, [receiver length])];

replaceOccurrencesOfString:withString:options:range:

CynicismRising
+1  A: 

Whenever you are looking for things in a string, try using NSScanner first.

NSString *yourString = @"ABCCDEDRFFED"; // For example
NSScanner *scanner = [NSScanner scannerWithString:yourString];

NSCharacterSet *charactersToCount = @"C" // For example
NSString *charactersFromString;

if (!([scanner scanCharactersFromSet:charactersToCount intoString:&charactersFromString])) {
    // No characters found
    NSLog(@"No characters found");
}

NSInteger characterCount = [charactersFromString length]; // should return 2 for this example
Abizern
i have not been able to get this to work at all. I'm trying to count the number of spaces.
lawrence
A: 

The example with the Scanner was crashing on iPhone. I found this solution :

NSString *yourString = @"ABCCDEDRFFED"; // For example
NSScanner *mainScanner = [NSScanner scannerWithString:yourString];
NSString *temp;
NSInteger numberOfChar=0;
while(![mainScanner isAtEnd])
{
   [mainScanner scanUpToString:@"C" intoString:&temp];
   numberOfChar++;
   [mainScanner scanString:@"C" intoString:nil];
}

It worked for me without crash. Hope it can help !

valeuf
+1  A: 

Your solution did not work for me, I added a condition in the loop to increment numberOfChar only if mainScanner has reach the end of the string :

NSString *yourString = @"ABCCDEDRFFED"; // For example
NSScanner *mainScanner = [NSScanner scannerWithString:yourString];
NSString *temp;
NSInteger numberOfChar=0;
while(![mainScanner isAtEnd])
{
   [mainScanner scanUpToString:@"C" intoString:&temp];
   if(![mainScanner isAtEnd]) {
      numberOfChar++;
      [mainScanner scanString:@"C" intoString:nil];
   }
}

Note that this is a quick fix, I don't have time to make an elegant solution...

pierrot3887