2

例如,我有一个 NSString "This is my question"。我想找到字符/子字符串的所有索引,"i"即在这种情况下,如果索引从 开始0,那么我想2,5,16作为我的答案。

4

5 回答 5

4

另一个答案有点矫枉过正。你为什么不简单地迭代这样的字符:

NSString *x = @"This is my question";

for (NSUInteger i=0;i<[x length];i++)
{
    if ([x characterAtIndex:i]=='i')
    {
        NSLog(@"found: %d", i);
    }
}

它准确地输出您的职位:

found: 2
found: 5
found: 16
于 2013-05-21T05:11:06.417 回答
1

我想建议我的解决方案。它是这样的:

NSString* str = @"This is my question";
NSArray* arr = [str componentsSeparatedByString: @"i"];
NSMutableArray* marr = [NSMutableArray arr];
NSInteger cnt = 0;
for (NSInteger i = 0; i < ([arr count]); i++)
{
    NSString* s = [arr objectAtIndex: i];
    cnt += [s length];
    [marr addObject: [NSNumber numberWithInt: cnt]];
    cnt += [@"i" length];
}

NSLog(@"%@", [marr description]);

控制台上:2 5 16

于 2013-05-21T05:22:59.140 回答
0

我不知道是否有任何内置函数可用于执行此操作。您可以使用此方法:

- (NSMutableArray *)indexOfCharacter:(char)c inString:(NSString*)string
{

    NSMutableArray *returnArray = [[NSMutableArray alloc] init];
    for(int i=0;i<string.length;i++)
    {
        if(c == [string characterAtIndex:i])
        {
           [returnArray addObject:[NSNumber numberWithInt:i]];
        }
    }
    return returnArray;
}
于 2013-05-21T05:13:44.487 回答
0

使用NSRange和循环以及一些字符串操作,您可以轻松地做到这一点。

        NSString *string = @"This is my question";
        NSString *substring = @"i";

        NSRange searchRange = NSMakeRange(0,string.length);
        NSRange foundRange;
        while (searchRange.location < string.length)
        {
            searchRange.length = string.length-searchRange.location;
            foundRange = [string rangeOfString:substring options:nil range:searchRange];
            if (foundRange.location != NSNotFound)
            {
                // found an occurrence of the char
                searchRange.location = foundRange.location+foundRange.length;
                NSLog(@"Location of '%@' is %d",substring,searchRange.location-1);
            }
        }

编辑

使用NSRegularExpression并且NSRange你可以这样做。

NSString *string = @"This is my question";
NSString *substring = @"i";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:substring
                                                                       options:0
                                                                         error:NULL];

[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                         NSRange range = [result range];
                         NSLog(@"Location of '%@' is %d",substring, range.location);
                     }];

输出是

Location of 'i' is 2
Location of 'i' is 5
Location of 'i' is 16
于 2013-05-21T05:12:03.230 回答
0

这是我尝试获取所需内容的无循环代码。我编写了这个盲代码,意思是未经测试等。它基本上是一个递归函数,但我认为它能让你大致了解。

- (NSArray *)getAllEyes:(NSString *)s index:(int)index) {
    if (!s || s.length <= 0 || index >= s.length) return [NSArray new];
    NSRange *r = [s rangeOfString(@"i") options:NSLiteralSearch range:NSMakeRange(index, s.length - index)];
    if (r.location == NSNotFound) {
        return [NSArray new];
    } else {
        NSMutableArray *array = [NSMutableArray new];
        [array addObject:@(r.location)];
        [array addObjectsFromArray:[self getAllEyes:s index:r.location + 1]];
        return array;
    }    
}

// usage:

NSArray *allEyes = [self getAllEyes:@""];
for (NSNumber *n in allEyes) {
    NSLog(@"i = %@", n);
}
于 2013-05-21T05:49:08.837 回答