欢迎光临朱凯的个人博客!
朱凯,a3.work

查找子字符串在父字符串中的所有位置

 发布时间: 2019-02-27 09:35      原创:  朱凯      评论: .       .人查看
+ (NSArray *)getSubStrLocations:(NSString *)content str:(NSString *)tab{
    int location = 0;
    NSMutableArray *locationArr = [NSMutableArray new];
    NSRange range = [content rangeOfString:tab];
    if (range.location == NSNotFound){
        return [locationArr copy];
    }
    //声明一个临时字符串,记录截取之后的字符串
    NSString *subStr = content;
    while (range.location != NSNotFound) {
        location += range.location;
        //记录位置
        NSNumber *number = [NSNumber numberWithUnsignedInteger:location];
        [locationArr addObject:number];
        //每次记录之后,把找到的字串截取掉
        subStr = [subStr substringFromIndex:range.location + range.length];
        location += tab.length;
        range = [subStr rangeOfString:tab];
    }
    return [locationArr copy];
}
分享是一种快乐,也是一种美德:
¥打开支付宝,扫码领红包¥ 红包

支付宝 aliPay

微信 wxPay