Iphone iterates over NSString occurrence substring

I would like to find all occurrences of a substring in an NSString and iterate over sequentially to make some changes to this NSString. How should I do it?

+6
iphone nsstring
Mar 08 2018-12-12T00:
source share
5 answers

What about

// find first occurrence of search string in source string NSRange range = [sourceString rangeOfString:@"searchString"]; while(range.location != NSNotFound) { // build a new string with your changed values range = [sourceString rangeOfString:@"searchString" options:0 range:NSMakeRange(range.location + 1, [sourceString length] - range.location - 1)]; } 

Or simply

 [sourceString stringByReplacingOccurrencesOfString:searchString withString:targetString]; 

if you want to change searchString to the same value in the source string.

+12
Mar 08 2018-12-12T00:
source share

I would say something like this:

 // Setup what you're searching and what you want to find NSString *string = @"abcabcabcabc"; NSString *toFind = @"abc"; // Initialise the searching range to the whole string NSRange searchRange = NSMakeRange(0, [string length]); do { // Search for next occurrence NSRange range = [string rangeOfString:toFind options:0 range:searchRange]; if (range.location != NSNotFound) { // If found, range contains the range of the current iteration // NOW DO SOMETHING WITH THE STRING / RANGE // Reset search range for next attempt to start after the current found range searchRange.location = range.location + range.length; searchRange.length = [string length] - searchRange.location; } else { // If we didn't find it, we have no more occurrences break; } } while (1); 
+6
Mar 08 2018-12-12T00:
source share

If you want to make changes, you can use:

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

but if that doesn't suit your needs, try the following:

- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block

+5
Mar 08 2018-12-12T00:
source share

Maybe you should take a look at the NSString Class Documentation .

Search for characters and substrings

 – rangeOfCharacterFromSet: – rangeOfCharacterFromSet:options: – rangeOfCharacterFromSet:options:range: – rangeOfString: – rangeOfString:options: – rangeOfString:options:range: – rangeOfString:options:range:locale: – enumerateLinesUsingBlock: – enumerateSubstringsInRange:options:usingBlock: 

Line splitting

 – componentsSeparatedByString: – componentsSeparatedByCharactersInSet: – stringByTrimmingCharactersInSet: – substringFromIndex: – substringWithRange: – substringToIndex: 
+1
Mar 08 2018-12-12T00:
source share

Turning around the @TheEye answer , I prepared this:

 @interface NSString (EnumerateOccurancesOfString) - (void)enumerateOccurancesOfString:(NSString *)string usingBlock:(void (^)(NSRange substringRange, BOOL *stop))block; @end 

-

 @implementation NSString (EnumerateOccurancesOfString) - (void)enumerateOccurancesOfString:(NSString *)string usingBlock:(void (^)(NSRange range, BOOL * _Nonnull stop))block { NSParameterAssert(block); NSRange range = [self localizedStandardRangeOfString:string]; if (range.location == NSNotFound) return; // Iterate all occurances of 'string' while (range.location != NSNotFound) { BOOL stop = NO; block(range, &stop); if (stop) { break; } // Continue the iteration NSRange nextRange = NSMakeRange(range.location + 1, self.length - range.location - 1); range = [self rangeOfString:string options:(NSStringCompareOptions)0 range:nextRange locale:[NSLocale currentLocale]]; // Will this sometimes conflict with the initial range obtained with -localizedStandardRangeOfString:? } } @end 
0
Aug 25 '16 at 16:42 on
source share



All Articles