查找子串出现次数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);
}

posted @ 2015-06-19 09:05  天使雨儿  阅读(243)  评论(0编辑  收藏  举报