//利用朴素算法进行字符串匹配
-(NSArray *)NaiveStringMatcher:(NSString *) matchStr String:(NSString *) str{
//在str中搜索matchStr并返回matchStr下标
NSInteger matchStrLehgth = matchStr.length;
NSInteger strLength = str.length;
NSMutableArray *indexArray = [[NSMutableArray alloc]init];
for (int index = 0; index <= (strLength - matchStrLehgth); index ++) {
NSRange range = {index,matchStr.length};
if ([matchStr isEqualToString:[str substringWithRange:range]]) {
[indexArray addObject:[NSString stringWithFormat:@"%d",index]];
}
}
return [NSArray arrayWithArray:indexArray];
}
该段代码会在str中搜索所有matchStr并存入集合返回