public class Test7 {
/**
* *
* 需求:统计大串中小串出现的次数
* 这里的大串和小串可以自己根据情况给出
*
*/
public static void main(String[] args) {
//定义大串
String max = "woaiheima,heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma";
//定义小串
String min = "heima";
//定义计数器变量
int count = 0;
//定义索引
int index = 0;
//定义循环,判断小串是否在大串中出现
while((index = max.indexOf(min)) != -1) {
count++; //计数器自增
max = max.substring(index + min.length());
}
System.out.println(count);
}
}
递归实现:
public class Test001 {
public static int COUNT = 0;
public static void main(String[] args) throws Exception {
String str = "abbbcdadeddweaabkkdlabdse";
String findStr = "ab";
int count = searchCount(str,findStr);
System.out.println(count);
}
public static int searchCount(String source,String findStr){
int index = source.indexOf(findStr);
if(index != -1){
COUNT++;
source = source.substring(index+findStr.length());
return searchCount(source, findStr);
}else{
return COUNT;
}
}
}