Iphone迭代NSString的子字符串出现

时间:2012-03-08 10:06:26

标签: iphone nsstring

我想在NSString中找到所有出现的子字符串,并逐个迭代以对该NSString进行一些更改。我该怎么办?

5 个答案:

答案 0 :(得分:12)

怎么样

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

或者只是

[sourceString stringByReplacingOccurrencesOfString:searchString withString:targetString];

如果要在搜索源字符串中的任何位置将searchString更改为相同的值。

答案 1 :(得分:7)

我会选择这样的东西:

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

答案 2 :(得分:5)

如果您想进行更改,可以使用:

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

但如果这不符合您的需求,请尝试以下方法:

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

答案 3 :(得分:1)

您可能希望查看NSString class Documentation

查找字符和子字符串

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

划分字符串

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

答案 4 :(得分:0)

扩展@TheEye' answer,我把它煮熟了:

@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