NSString中子字符串的出现次数?

时间:2010-01-30 04:10:05

标签: objective-c cocoa nsstring substring

如何获取NSString(例如@"cake")出现在较大的NSString中的次数(例如@"Cheesecake, apple cake, and cherry pie")?

我需要在很多字符串上执行此操作,因此无论使用何种方法都需要相对较快。

谢谢!

13 个答案:

答案 0 :(得分:98)

这未经过测试,但应该是一个良好的开端。

NSUInteger count = 0, length = [str length];
NSRange range = NSMakeRange(0, length); 
while(range.location != NSNotFound)
{
  range = [str rangeOfString: @"cake" options:0 range:range];
  if(range.location != NSNotFound)
  {
    range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
    count++; 
  }
}

答案 1 :(得分:70)

如下所示的正则表达式应该在没有循环交互的情况下完成工作......

<强>被修改

NSString *string = @"Lots of cakes, with a piece of cake.";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"cake" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"Found %i",numberOfMatches);

仅适用于iOS 4.x和上级。

答案 2 :(得分:43)

正在寻找一种比我更好的方法,但这是另一个例子:

NSString *find = @"cake";
NSString *text = @"Cheesecake, apple cake, and cherry pie";

NSInteger strCount = [text length] - [[text stringByReplacingOccurrencesOfString:find withString:@""] length];
strCount /= [find length];

我想知道哪一个更有效。

我提出了NSString类别以便更好地使用:

// NSString+CountString.m

@interface NSString (CountString)
- (NSInteger)countOccurencesOfString:(NSString*)searchString;
@end

@implementation NSString (CountString)
- (NSInteger)countOccurencesOfString:(NSString*)searchString {
    NSInteger strCount = [self length] - [[self stringByReplacingOccurrencesOfString:searchString withString:@""] length];
    return strCount / [searchString length];
}
@end

简单地称之为:

[text countOccurencesOfString:find];

可选  您可以通过定义options:

来修改它以搜索不区分大小写

答案 3 :(得分:23)

有几种方法可以做到。您可以迭代地调用rangeOfString:options:range:,或者您可以执行以下操作:

NSArray * portions = [aString componentsSeparatedByString:@"cake"];
NSUInteger cakeCount = [portions count] - 1;

编辑我再次考虑这个问题,我写了一个线性时间算法来进行搜索(线性到干草堆字符串的长度):

+ (NSUInteger) numberOfOccurrencesOfString:(NSString *)needle inString:(NSString *)haystack {
    const char * rawNeedle = [needle UTF8String];
    NSUInteger needleLength = strlen(rawNeedle);

    const char * rawHaystack = [haystack UTF8String];
    NSUInteger haystackLength = strlen(rawHaystack);

    NSUInteger needleCount = 0;
    NSUInteger needleIndex = 0;
    for (NSUInteger index = 0; index < haystackLength; ++index) {
        const char thisCharacter = rawHaystack[index];
        if (thisCharacter != rawNeedle[needleIndex]) {
            needleIndex = 0; //they don't match; reset the needle index
        }

        //resetting the needle might be the beginning of another match
        if (thisCharacter == rawNeedle[needleIndex]) {
            needleIndex++; //char match
            if (needleIndex >= needleLength) {
                needleCount++; //we completed finding the needle
                needleIndex = 0;
            }
        }
    }

    return needleCount;
}

答案 4 :(得分:11)

更快速地输入,但可能效率较低的解决方案。

- (int)numberOfOccurencesOfSubstring:(NSString *)substring inString:(NSString*)string
{
    NSArray *components = [string componentsSeparatedByString:substring];
    return components.count-1; // Two substring will create 3 separated strings in the array.
}

答案 5 :(得分:3)

这是作为NSString的扩展而完成的版本(与Matthew Flaschen的回答相同):

@interface NSString (my_substr_search)
- (unsigned) countOccurencesOf: (NSString *)subString;
@end
@implementation NSString (my_substring_search)
- (unsigned) countOccurencesOf: (NSString *)subString {
    unsigned count = 0;
    unsigned myLength = [self length];
    NSRange uncheckedRange = NSMakeRange(0, myLength);
    for(;;) {
        NSRange foundAtRange = [self rangeOfString:subString
                                           options:0
                                             range:uncheckedRange];
        if (foundAtRange.location == NSNotFound) return count;
        unsigned newLocation = NSMaxRange(foundAtRange); 
        uncheckedRange = NSMakeRange(newLocation, myLength-newLocation);
        count++;
    }
}
@end
<somewhere> {
    NSString *haystack = @"Cheesecake, apple cake, and cherry pie";
    NSString *needle = @"cake";
    unsigned count = [haystack countOccurencesOf: needle];
    NSLog(@"found %u time%@", count, count == 1 ? @"" : @"s");
}

答案 6 :(得分:3)

如果您想计算单词,而不仅仅是子字符串,请使用CFStringTokenizer

答案 7 :(得分:3)

这是另一个版本作为NSString上的类别:

-(NSUInteger) countOccurrencesOfSubstring:(NSString *) substring {
    if ([self length] == 0 || [substring length] == 0)
        return 0;

    NSInteger result = -1;
    NSRange range = NSMakeRange(0, 0);
    do {
        ++result;
        range = NSMakeRange(range.location + range.length,
                            self.length - (range.location + range.length));
        range = [self rangeOfString:substring options:0 range:range];
    } while (range.location != NSNotFound);
    return result;
}

答案 8 :(得分:3)

Swift解决方案将是:

var numberOfSubstringAppearance = 0
let length = count(text)
var range: Range? = Range(start: text.startIndex, end: advance(text.startIndex, length))

while range != nil {

    range = text.rangeOfString(substring, options: NSStringCompareOptions.allZeros, range: range, locale: nil)

    if let rangeUnwrapped = range {

        let remainingLength = length - distance(text.startIndex, rangeUnwrapped.endIndex)
        range = Range(start: rangeUnwrapped.endIndex, end: advance(rangeUnwrapped.endIndex, remainingLength))
        numberOfSubstringAppearance++
     }
}

答案 9 :(得分:1)

Matthew Flaschen的回答对我来说是一个好的开始。以下是我最终以方法的形式使用的内容。我对循环采取了略微不同的方法。已经使用传递给stringToCount和text的空字符串以及stringToCount作为文本中的第一个和/或最后一个字符进行了测试。

我经常使用这个方法计算传递文本中的段落(即stringToCount = @“\ r”)。

希望这对某人有用。

    - (int)countString:(NSString *)stringToCount inText:(NSString *)text{
        int foundCount=0;
        NSRange range = NSMakeRange(0, text.length);
        range = [text rangeOfString:stringToCount options:NSCaseInsensitiveSearch range:range locale:nil];
        while (range.location != NSNotFound) {
            foundCount++;
            range = NSMakeRange(range.location+range.length, text.length-(range.location+range.length));
            range = [text rangeOfString:stringToCount options:NSCaseInsensitiveSearch range:range locale:nil];
        }

        return foundCount;
   }

示例调用假设该方法位于名为myHelperClass ...

的类中
int foundCount = [myHelperClass countString:@"n" inText:@"Now is the time for all good men to come to the aid of their country"];

答案 10 :(得分:0)

for(int i =0;i<htmlsource1.length-search.length;i++){
  range = NSMakeRange(i,search.length);
  checker = [htmlsource1 substringWithRange:range];

  if ([search isEqualToString:checker]) {
   count++;

  }

 }

答案 11 :(得分:-1)

没有内置方法。我建议返回一个c字符串,并使用一个常见的c字符串样式算法进行子字符串计数...如果你真的需要这个快速的话。

如果你想留在目标C,这link可能会有所帮助。它描述了NSString的基本子字符串搜索。如果你使用范围,调整和计数,那么你将有一个“纯粹的”Objective C解决方案......虽然很慢。

答案 12 :(得分:-1)

-(IBAction)search:(id)sender{

  int  maincount = 0;
    for (int i=0; i<[self.txtfmainStr.text length]; i++) {
        char c =[self.substr.text characterAtIndex:0];
        char cMain =[self.txtfmainStr.text characterAtIndex:i];
        if (c == cMain) {
          int  k=i;
            int count=0;
            for (int j = 0; j<[self.substr.text length]; j++) {

                if (k ==[self.txtfmainStr.text length]) {
                    break;
                }

                if ([self.txtfmainStr.text characterAtIndex:k]==[self.substr.text characterAtIndex:j]) {

                    count++;
                }                

                if (count==[self.substr.text length]) {
                    maincount++;
                }

                k++;
            }


        }

        NSLog(@"%d",maincount);
    }

}