39

我有一个NSStringorNSMutableString并且想要获取特定字符的出现次数。

我需要为相当多的字符(在这种情况下为大写英文字符)执行此操作,所以它会很快。

4

10 回答 10

102

您可以在一行中完成此操作。例如,这会计算空格数:

NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:@" "] count] - 1;
于 2011-03-10T11:56:20.587 回答
26

在 NSString 上试试这个类别:

@implementation NSString (OccurrenceCount)

- (NSUInteger)occurrenceCountOfCharacter:(UniChar)character
{
    CFStringRef selfAsCFStr = (__bridge CFStringRef)self;

    CFStringInlineBuffer inlineBuffer;
    CFIndex length = CFStringGetLength(selfAsCFStr);
    CFStringInitInlineBuffer(selfAsCFStr, &inlineBuffer, CFRangeMake(0, length));

    NSUInteger counter = 0;

    for (CFIndex i = 0; i < length; i++) {
        UniChar c = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, i);
        if (c == character) counter += 1;
    }

    return counter;
}

@end

这比该componentsSeparatedByString:方法快大约 5 倍。

于 2013-04-11T11:07:56.353 回答
16

replaceOccurrencesOfString:withString:options:range:将返回在 a 中替换的字符数NSMutableString

[string replaceOccurrencesOfString:@"A" 
                        withString:@"B" 
                           options:NSLiteralSearch 
                             range:NSMakeRange(0, [receiver length])];
于 2009-06-02T07:49:02.730 回答
7

每当您在 a 中寻找东西时NSString,请先尝试使用NSScanner

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

NSCharacterSet *charactersToCount = [NSCharacterSet characterSetWithCharactersInString:@"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];
于 2009-06-02T12:59:08.440 回答
6

现在我想到的第一件事就是:NSCountedSet

NSString *string = @"AAATTC";

NSMutableArray *array = [NSMutableArray array];

[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);
}

日志:

C: 1
G: 0
A: 3
T: 2
于 2014-09-28T18:24:37.653 回答
5

不同 Objective-C 解决方案的性能比较。

假设以下所有方法都是 NSString 扩展(内部@implementation NSString (CountOfOccurrences))。

作为示例,我使用了一个随机生成的长度为 100000000 的字符串,其中使用了所有拉丁字符(CharacterSet(charactersIn: "\u{0020}"..."\u{036F}")在 Swift 中)。而要数的字符是@"a"

在 Xcode 10.3 上以发布配置在模拟器上执行的测试。

快速解决方案(精确的逐个字符等效)

计算一个字符有两种方法:使用NSLiteralSearch或不使用。计数会有所不同,性能会受到根本影响。为了获得最快的结果,我们将执行精确的逐个字符等效。以下四种解决方案给出了非常接近的性能结果。

1.最快的解决方案:CynicismRising答案的改编。

使用replaceOccurrencesOfString:withString:options:range:. 这是所有场景中最快的解决方案:即使您替换NSLiteralSearchkNilOptions,您仍然比 pierrot3887 扫描仪解决方案更快。

- (NSUInteger)countOccurrencesOfString:(NSString *)stringToFind
{
    return [[NSMutableString stringWithString:self] replaceOccurrencesOfString:stringToFind
                                                                    withString:stringToFind
                                                                       options:NSLiteralSearch
                                                                         range:NSMakeRange(0, self.length)];
}

2.第二快,CynicismRising 答案的另一个改编。

使用stringByReplacingOccurrencesOfString:withString:options:range:.

- (NSUInteger)countOccurrencesOfString:(NSString *)stringToFind
{
    NSString *strippedString = [self stringByReplacingOccurrencesOfString:stringToFind
                                                               withString:@""
                                                                  options:NSLiteralSearch
                                                                    range:NSMakeRange(0, self.length)];
    return (self.length - strippedString.length) / stringToFind.length;
}

3. 第三快,雅克解。

使用CFStringGetCharacterFromInlineBuffer. 请参阅https://stackoverflow.com/a/15947190/1033581

4. 第四快,将我的 Swift 答案转换为 Objective-C。

使用rangeOfString:options:range:.

- (NSUInteger)countOccurrencesOfString:(NSString *)stringToFind
{
    //assert(stringToFind.length);
    NSUInteger count = 0;
    NSRange searchRange = NSMakeRange(0, self.length);
    NSRange foundRange;
    while ((void)(foundRange = [self rangeOfString:stringToFind options:NSLiteralSearch range:searchRange]), foundRange.length) {
        count += 1;
        NSUInteger loc = NSMaxRange(foundRange);
        searchRange = NSMakeRange(loc, self.length - loc);
    }
    return count;
}

缓慢的解决方案

以下解决方案不使用NSLiteralSearch也不执行精确的逐个字符等效。前两个可能比快速解决方案慢 10 倍,最后一个可能慢 100 倍。

5.慢解决:pierrot3887答案的改编

使用scanUpToString:intoString:. 太糟糕了,NSScanner它没有提供精确的逐个字符等效的选项。

- (NSUInteger)countOccurrencesOfString:(NSString *)stringToFind
{
    NSScanner *scanner = [NSScanner scannerWithString:self];
    scanner.charactersToBeSkipped = nil;
    scanner.caseSensitive = YES;
    NSUInteger numberOfOccurrences = 0;
    while (!scanner.isAtEnd) {
        [scanner scanUpToString:stringToFind intoString:nil];
        if (!scanner.isAtEnd) {
            numberOfOccurrences++;
            [scanner scanString:stringToFind intoString:nil];
        }
    }
    return numberOfOccurrences;
}

6.较慢的解决方案:gbaor解决方案

使用componentsSeparatedByString:. 关于 doable in one line 的论点,请注意上面给出的最快解决方案也是 one liner。

- (NSUInteger)countOccurrencesOfString:(NSString *)stringToFind
{
    return [self componentsSeparatedByString:stringToFind].count - 1;
}

7.最慢的解决方案:vikingosegundo answer的改编

使用enumerateSubstringsInRange:options:usingBlock:.

- (NSUInteger)countOccurrencesOfCharacter:(NSString *)characterToFind
{
    __block NSUInteger counter = 0;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        if ([characterToFind isEqualToString:substring]) counter += 1;
    }];
    return counter;
}
于 2019-09-10T10:54:31.530 回答
2

您的解决方案对我不起作用,我在循环中添加了一个条件,仅当 mainScanner 已到达字符串末尾时才增加 numberOfChar :

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];
   }
}

请注意,这是一个快速修复,我没有时间做出优雅的解决方案......

于 2010-04-08T15:56:52.737 回答
1

我可能会使用

NSString rangeOfCharacterFromSet:

或者

rangeOfCharacterFromSet:options:range::

其中 set 是您要搜索的字符集。它返回与集合匹配的第一个字符的位置。保留数组或字典并增加字符计数,然后重复。

于 2009-06-02T06:14:25.940 回答
1

扫描仪的示例在 iPhone 上崩溃。我找到了这个解决方案:

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];
}

它对我有用,没有崩溃。希望它可以帮助!

于 2010-01-15T11:39:37.767 回答
0

这是一个用于 NSRange、Range、String 和 NSString 的 Swift 3 工作版本!享受 :)

/// All ranges using NSString and NSRange
/// Is usually used together with NSAttributedString

extension NSString {
    public func ranges(of searchString: String, options: CompareOptions = .literal, searchRange: NSRange? = nil) -> [NSRange] {
        let searchRange = searchRange ?? NSRange(location: 0, length: self.length)
        let subRange = range(of: searchString, options: options, range: searchRange)
        if subRange.location != NSNotFound {

            let nextRangeStart = subRange.location + subRange.length
            let nextRange = NSRange(location: nextRangeStart, length: searchRange.location + searchRange.length - nextRangeStart)
            return [subRange] + ranges(of: searchString, options: options, searchRange: nextRange)
        } else {
            return []
        }
    }
}

/// All ranges using String and Range<Index>
/// Is usually used together with NSAttributedString

extension String {
    public func ranges(of searchString: String, options: CompareOptions = [], searchRange: Range<Index>? = nil ) -> [Range<Index>] {
        if let range = range(of: searchString, options: options, range: searchRange, locale: nil) {

            let nextRange = range.upperBound..<(searchRange?.upperBound ?? endIndex)
            return [range] + ranges(of: searchString, searchRange: nextRange)
        } else {
            return []
        }
    }
}
于 2016-08-30T15:18:29.530 回答