//循環(huán)方法
///核心思想是找到相同字符串就對字符串進(jìn)行截取,然后對剩余字符串進(jìn)行
- (NSInteger)getCountFrom:(NSString)allStr with:(NSString)cutStr{
//空的時候直接返回0
if (allStr.length == 0) {
return 0;
}
//接下來是處理字符串
NSString *leftStr = allStr;
NSInteger includeCount = 0;
//顯得我嚴(yán)謹(jǐn),其實(shí)這里循環(huán)條件無所謂
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;
}
//遞歸方法
///核心思想是找到相同字符串就對字符串進(jìn)行截取,然后對剩余字符串進(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;
}