查找子串出现次数OC
- (NSInteger)countOfSubstring:(NSString *)substring inString:(NSString *)s
{
NSArray *ary=[s componentsSeparatedByString:substring];
NSString *str=[ary componentsJoinedByString:@""];
NSUInteger scount=s.length;
NSUInteger strcount=str.length;
NSUInteger count=(scount-strcount)/substring.length;
return count;
}
+(void)test
{
NSString *str1=@"abcaaabcccabcbbabc";
NSString *str2=@"abc";
Rten *ten=[[Rten alloc]init];
NSInteger count=[ten countOfSubstring:str2 inString:str1];
NSLog(@"出现了%ld次",(long)count);
}