Ios 特定索引后字符的出现

Ios 特定索引后字符的出现,ios,objective-c,nsstring,Ios,Objective C,Nsstring,有什么方法可以实现JAVA函数intindexof(intch,intfromindex)的功能吗 让我解释一下: NSString *steSample = @"This is sample test string"; 现在我想得到i的索引,但是在第二个索引之后。我怎样才能做到这一点 提前谢谢我会这样做: NSString *steSample = @"This is sample test string"; NSUInteger count = 0, length = [steSample

有什么方法可以实现JAVA函数intindexof(intch,intfromindex)的功能吗

让我解释一下:

NSString *steSample = @"This is sample test string";
现在我想得到i的索引,但是在第二个索引之后。我怎样才能做到这一点


提前谢谢

我会这样做:

NSString *steSample = @"This is sample test string";
NSUInteger count = 0, length = [steSample length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
    range = [steSample rangeOfString: @"i" options:0 range:range];
    if(range.location != NSNotFound)
    {
        range = NSMakeRange(range.location + range.length, length - (range.location + range.length));

        count++;
        if (count == 2)
        {
            NSLog(@"%d", range.location); // print 6 which is location of second 'i'
        }
    }
}
NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23
注意:您可以重新排列实际的
if
语句,它当前会“跳过”第一次出现的语句,并打印其余的语句,但可以根据您的进一步愿望进行定制


我的控制台显示如下内容:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23

我会这样做:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23
注意:您可以重新排列实际的
if
语句,它当前会“跳过”第一次出现的语句,并打印其余的语句,但可以根据您的进一步愿望进行定制


我的控制台显示如下内容:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23

我会这样做:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23
注意:您可以重新排列实际的
if
语句,它当前会“跳过”第一次出现的语句,并打印其余的语句,但可以根据您的进一步愿望进行定制


我的控制台显示如下内容:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23

我会这样做:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23
注意:您可以重新排列实际的
if
语句,它当前会“跳过”第一次出现的语句,并打印其余的语句,但可以根据您的进一步愿望进行定制


我的控制台显示如下内容:

NSString *_sample = @"This is sample test string";
NSError *_error;
NSRegularExpression *_regExp = [NSRegularExpression regularExpressionWithPattern:@"i" options:NSRegularExpressionCaseInsensitive error:&_error];
NSArray *_matches = [_regExp matchesInString:_sample options:NSMatchingReportCompletion range:NSMakeRange(0, _sample.length)];
[_matches enumerateObjectsUsingBlock:^(NSTextCheckingResult * result, NSUInteger idx, BOOL *stop) {
    if (idx == 0) {
        NSLog(@"ignoring first occurance...");
    } else {
        NSLog(@"occurance's index : %d, character's index in string : %d", idx, result.range.location); // that line is simplified for your problem
    }
}];
ignoring first occurance...
occurance's index : 1, character's index in string : 5
occurance's index : 2, character's index in string : 23

regex方法对我来说太复杂了:)

我们不能修剪一下然后再找吗

那将是3行

包装在一个类别中:

#import <Foundation/Foundation.h>

@interface NSString (Extend)
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index;
@end
@implementation NSString (Extend) 
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index {
    id str = [self substringFromIndex:index];

    NSUInteger i = [str rangeOfString:needle].location;
    return i==NSNotFound ? i : i+index;
}
@end

regex方法对我来说太复杂了:)

我们不能修剪一下然后再找吗

那将是3行

包装在一个类别中:

#import <Foundation/Foundation.h>

@interface NSString (Extend)
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index;
@end
@implementation NSString (Extend) 
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index {
    id str = [self substringFromIndex:index];

    NSUInteger i = [str rangeOfString:needle].location;
    return i==NSNotFound ? i : i+index;
}
@end

regex方法对我来说太复杂了:)

我们不能修剪一下然后再找吗

那将是3行

包装在一个类别中:

#import <Foundation/Foundation.h>

@interface NSString (Extend)
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index;
@end
@implementation NSString (Extend) 
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index {
    id str = [self substringFromIndex:index];

    NSUInteger i = [str rangeOfString:needle].location;
    return i==NSNotFound ? i : i+index;
}
@end

regex方法对我来说太复杂了:)

我们不能修剪一下然后再找吗

那将是3行

包装在一个类别中:

#import <Foundation/Foundation.h>

@interface NSString (Extend)
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index;
@end
@implementation NSString (Extend) 
-(NSUInteger)indexOfSubstring:(NSString*)needle afterIndex:(NSUInteger)index {
    id str = [self substringFromIndex:index];

    NSUInteger i = [str rangeOfString:needle].location;
    return i==NSNotFound ? i : i+index;
}
@end


第二个
i
的位置是5。在成功找到字符后,但在记录值之前,您似乎正在修改范围。我也在考虑这种方法,但我认为使用
[NSString enumerateSubstringsInRange:options:usingBlock:
可能不那么复杂。第二个
I
的位置是5。在成功找到字符后,但在记录值之前,您似乎正在修改范围。我也在考虑这种方法,但我认为使用
[NSString enumerateSubstringsInRange:options:usingBlock:
可能不那么复杂。第二个
I
的位置是5。在成功找到字符后,但在记录值之前,您似乎正在修改范围。我也在考虑这种方法,但我认为使用
[NSString enumerateSubstringsInRange:options:usingBlock:
可能不那么复杂。第二个
I
的位置是5。在成功找到字符后,但在记录值之前,您似乎正在修改范围。我也在考虑这种方法,但我认为使用
[NSString enumerateSubstringsInRange:options:usingBlock:
可能不那么复杂。太好了!!谢谢@holexfind这太复杂了——为什么我们要编译正则表达式模式even@Daij-Djan,阅读或理解起来很复杂,或者你的意思是什么?实际上我的意思是——对于一个相当简单的任务来说,代码太多了;)有点像:“用大锤敲坚果”太棒了!!谢谢@holexfind这太复杂了——为什么我们要编译正则表达式模式even@Daij-Djan,阅读或理解起来很复杂,或者你的意思是什么?实际上我的意思是——对于一个相当简单的任务来说,代码太多了;)有点像:“用大锤敲坚果”太棒了!!谢谢@holexfind这太复杂了——为什么我们要编译正则表达式模式even@Daij-Djan,阅读或理解起来很复杂,或者你的意思是什么?实际上我的意思是——对于一个相当简单的任务来说,代码太多了;)有点像:“用大锤敲坚果”太棒了!!谢谢@holexfind这太复杂了——为什么我们要编译正则表达式模式even@Daij-Djan,阅读或理解起来很复杂,或者你的意思是什么?实际上我的意思是——对于一个相当简单的任务来说,代码太多了;)有点像:“用大锤敲碎坚果”这正是我要找的。令人惊叹的!!这正是我要找的。令人惊叹的!!这正是我要找的。令人惊叹的!!这正是我要找的。令人惊叹的!!