简体   繁体   English

Iphone迭代NSString的子字符串出现

[英]Iphone iterate over substring occurrences of a NSString

I would like to find all occurrences of a substring in a NSString, and iterate one by one to do some changes to that NSString. 我想在NSString中找到所有出现的子字符串,并逐个迭代以对该NSString进行一些更改。 How should I do it? 我该怎么办?

How 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 just 要不就

[sourceString stringByReplacingOccurrencesOfString:searchString withString:targetString];

if you want to change the searchString to the same value everywhere in the source string. 如果要将searchString更改为源字符串中的任何位置的相同值。

I would go with 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);

If you want to do changes, you could use: 如果要进行更改,可以使用:

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

but if that doesn't fit your needs try this: 但如果这不符合您的需求,请尝试以下方法:

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

You might want to have a look on NSString class Documentation . 您可能想查看NSString类文档

Finding Characters and Substrings 查找字符和子字符串

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

Dividing Strings 划分字符串

– componentsSeparatedByString:
– componentsSeparatedByCharactersInSet:
– stringByTrimmingCharactersInSet:
– substringFromIndex:
– substringWithRange:
– substringToIndex:

Expanding on @TheEye's answer , I cooked this up: 在@ TheEye的回答中扩展,我把它煮熟了:

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM