Number of substring occurrences in NSString?

How can I get the number of times an NSString (e.g. @"cake" ) appears in a larger NSString (e.g. @"Cheesecake, apple cake, and cherry pie" )?

I need to do this on multiple lines, so any method I use should be relatively fast.

Thank!

+50
substring objective-c cocoa nsstring
Jan 30 '10 at 4:10
source share
13 answers

This is not tested, but should be a good start.

 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++; } } 
+93
Jan 30 '10 at 4:59
source share

A regular expression like the one below should do the job without loop interaction ...

Edited

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

Available only for iOS 4.x and for superiors.

+63
Jun 19 2018-12-12T00:
source share

I was looking for a better method than mine, but here is another example:

 NSString *find = @"cake"; NSString *text = @"Cheesecake, apple cake, and cherry pie"; NSInteger strCount = [text length] - [[text stringByReplacingOccurrencesOfString:find withString:@""] length]; strCount /= [find length]; 

I would like to know which one is more efficient.

And I made the NSString category for better use:

 // 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 

just name it:

 [text countOccurencesOfString:find]; 

Optional: you can change it to search for case insensitivity by specifying options:

+37
Mar 15 2018-11-11T00:
source share

There are several ways to do this. You can iteratively call rangeOfString:options:range: or you could do something like:

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

EDIT I thought about this again, and I wrote a linear time algorithm for searching (linear along the length of the hay line):

 + (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; } 
+21
Jan 30 '10 at 4:25
source share

Faster to introduce, but probably a less effective solution.

 - (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. } 
+11
Jul 19 '13 at 18:53 on
source share

Here is a version made as an extension to NSString (same idea as Matthew Flaschen's answer):

 @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"); } 
+3
Jan 30 '10 at 5:20
source share

If you want to count words, not just substrings, use CFStringTokenizer .

+3
Mar 03 '10 at 15:00
source share

Here's another version as a category on 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; } 
+3
Apr 11 '13 at 15:08
source share

Fast decision:

 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++ } } 
+3
Sep 17 '15 at 7:59
source share

Matthew Flashen's answer was a good start for me. Here is what I ended up using in the form of a method. I made a slightly different approach to the cycle. This has been tested with empty strings passed to stringToCount and text, and with stringToCount, which occur as the first and / or last characters in the text.

I use this method regularly to count paragraphs in the submitted text (for example, stringToCount = @ "\ r").

I hope that someone will like it.

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

An example call, assuming the method is in a class named myHelperClass ...

 int foundCount = [myHelperClass countString:@"n" inText:@"Now is the time for all good men to come to the aid of their country"]; 
+1
Jan 07 2018-11-11T00:
source share
 for(int i =0;i<htmlsource1.length-search.length;i++){ range = NSMakeRange(i,search.length); checker = [htmlsource1 substringWithRange:range]; if ([search isEqualToString:checker]) { count++; } } 
0
May 02 '10 at 12:49
source share

There is no built-in method. I would suggest returning the c-string and using the general c-string style algorithm to count the substring ... if you really need it to be fast.

If you want to stay in Objective-C, this one might help. It describes a basic substring search for NSString. If you work with ranges, tune and count, then you will have a β€œclean” Objective-C solution ... albeit a slow one.

-one
Jan 30 '10 at 4:20
source share
 -(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); } } 
-one
Oct 25 '13 at 14:15
source share



All Articles