//循環(huán)方法
///核心思想是找到相同字符串就對(duì)字符串進(jìn)行截取苛白,然后對(duì)剩余字符串進(jìn)行
- (NSInteger)getCountFrom:(NSString)allStr with:(NSString)cutStr{
//空的時(shí)候直接返回0
if (allStr.length == 0) {
return 0;
}
//接下來(lái)是處理字符串
NSString *leftStr = allStr;
NSInteger includeCount = 0;
//顯得我嚴(yán)謹(jǐn)倾芝,其實(shí)這里循環(huán)條件無(wú)所謂
while (leftStr.length >= cutStr.length) {
NSRange range = [leftStr rangeOfString:cutStr];
if (range.location != NSNotFound) {
includeCount += 1;
leftStr = [leftStr substringFromIndex:range.location + range.length];
}else{
return includeCount;
}
}
return includeCount;
}
//遞歸方法
///核心思想是找到相同字符串就對(duì)字符串進(jìn)行截取,然后對(duì)剩余字符串進(jìn)行
- (NSInteger)getCountFrom1:(NSString)allStr with:(NSString)cutStr{
static NSInteger includeCount = 0;
//直接處理字符串
if (allStr.length < cutStr.length) {
return includeCount;
}
NSRange range = [allStr rangeOfString:cutStr];
if (range.location != NSNotFound) {
includeCount += 1;
[self getCountFrom1:[allStr substringFromIndex:range.location + range.length] with:cutStr];
}else{
return includeCount;
}
return includeCount;
}