public class 大串小串做法一 {
public static void main(String[] args) {
String max = "woaiheima,heimabutongyubaima,buguanshiheimahaishi1baima,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 大串小串做法二 {
public static void main(String[] args) {
String maxStr = "woaiheima,heimabutongyubaima,buguanshiheimahaishi1baima,zhaodaogongzuojiushihaoma";
String minStr = "heima";
//int count = (大串的长度-新串的长度)/小串的长度
int count = (maxStr.length()-maxStr.replace(minStr, "").length())/minStr.length();
System.out.println(count);
}
} |
|