Number of Occurrences of a Character in NSString

2019-01-07 17:47发布

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.

9条回答
迷人小祖宗
2楼-- · 2019-01-07 18:12

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.

查看更多
太酷不给撩
3楼-- · 2019-01-07 18:17

replaceOccurrencesOfString:withString:options:range: will return the number of characters replaced in a NSMutableString.

[string replaceOccurrencesOfString:@"A" 
                        withString:@"B" 
                           options:NSLiteralSearch 
                             range:NSMakeRange(0, [receiver length])];
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-07 18:21

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 !

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-07 18:24

Whenever you are looking for things in a NSString, 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");
}

// should return 2 for this
NSInteger characterCount = [charactersFromString length];
查看更多
男人必须洒脱
6楼-- · 2019-01-07 18:25

Nowadays the first thing that come to my mind for something like that: NSCountedSet

NSString *string =@"AAATTC";

NSMutableArray *array = [@[] mutableCopy];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    [array addObject:substring];
}] ;
NSCountedSet * set = [[NSCountedSet alloc] initWithArray:array];

for (NSString *nucleobase in @[@"C", @"G", @"A", @"T"]){
    NSUInteger count =  [set countForObject:nucleobase];
    NSLog(@"%@: %lu", nucleobase, (unsigned long)count);
}

logs:

C: 1
G: 0
A: 3
T: 2
查看更多
Animai°情兽
7楼-- · 2019-01-07 18:26

You can do this in one line. For example, this counts the number of spaces:

NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:@" "] count] - 1;
查看更多
登录 后发表回答