如何计算字符串中所有字符的出现次数?

时间:2014-05-15 03:22:19

标签: ios objective-c string

我试图找到字符串中每个字符的使用次数。例如,在字符串"哇"我想算一下这个角色的次数" w"使用和字符" o"的次数。用来。然后我想将这些字符添加到NSMutableArray中。是否有一种编程方式来计算所有特定字符的使用次数?要获取NSString中所有字符的出现次数?或者我是否必须分别完成计算每个角色出现次数的过程?

3 个答案:

答案 0 :(得分:3)

请参阅iOS - Most efficient way to find word occurrence count in a string

NSString     *string     = @"wow";
    NSCountedSet *countedSet = [NSCountedSet new];

    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationLocalized
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                                // This block is called once for each word in the string.
                                [countedSet addObject:substring];

                                // If you want to ignore case, so that "this" and "This"
                                // are counted the same, use this line instead to convert
                                // each word to lowercase first:
                                // [countedSet addObject:[substring lowercaseString]];
                            }];

    NSLog(@"%@", countedSet);
    NSLog(@"%@", [countedSet allObjects]);
    NSLog(@"%d", [countedSet countForObject:@"w"]);

答案 1 :(得分:0)

确切的答案取决于一些问题 -

  • 您是否只想计算字符a-z还是想要标点符号?
  • 您需要统计unicode字符还是8位字符?
  • 案例重要即。是不同的?

假设您只想计算8位,a-z独立于大小写,您可以使用类似的东西 -

- (NSArray *)countCharactersInString:(NSString *)inputString
{
    NSMutableArray *result=[[NSMutableArray alloc]initWithCapacity:26];
    for (int i=0;i<26;i++) {
        [result addObject:[NSNumber numberWithInt:0]];
    }

    for (int i=0;i<[inputString length];i++)
    {
        unichar c=[inputString characterAtIndex:i];
        c=tolower(c);
        if (isalpha(c))
        {
            int index=c-'a';
            NSNumber *count=[result objectAtIndex:index];
            [result setObject:[NSNumber numberWithInt:[count intValue]+1]  atIndexedSubscript:index];
        }
    }

    return (result);
}

另一种方法是使用NSCountedSet - 它处理所有的字符缩放等,但是会稀疏&#39; - 字符串中没有字符的条目。此外,下面的实现区分大小写 - W与w不同。

- (NSCountedSet *)countCharactersInString:(NSString *)inputString
{
  NSCountedSet *result=[[NSCountedSet alloc]init];
  for (int i=0;i<[inputString length];i++)
  {
    NSString *c=[inputString substringWithRange:NSMakeRange(i,1)];
    [result addObject:c];
  }
  return result;
}

答案 2 :(得分:0)

NSString *str = @"Program to Find the Frequency of Characters in a String";

NSMutableDictionary *frequencies = [[NSMutableDictionary alloc]initWithCapacity:52];

initWithCapacity:52 - 容量可能更多取决于字符集(目前:a-z,A-Z)

for (short i=0; i< [str length]; i++){
    short index = [str characterAtIndex:i];

    NSString *key   = [NSString stringWithFormat:@"%d",index];
    NSNumber *value = @1;

    short frequencyCount=0;
    if ([frequencies count] > 0 && [frequencies valueForKey:key]){

        frequencyCount = [[frequencies valueForKey:key] shortValue];
        frequencyCount++;

        value = [NSNumber numberWithShort:frequencyCount];

        [frequencies setValue:value forKey:key];
    }
    else{
       [frequencies setValue:value forKey:key];
    }
}

显示字符串

中每个字符的出现次数
[frequencies enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    NSString *ky = (NSString*)key;
    NSNumber *value = (NSNumber*)obj;

    NSLog(@"%c\t%d", ([ky intValue]), [value shortValue]);
}];