1.键盘录入一个大字符串,再录入一个小字符串
2.统计小字符串在大字符串中出现的次数
3.代码运行打印格式:
请输入大字符串:woaichina,chinabutongyubaima,wulunchinahaishijpan,zhaodaogongzuojiushihaoma
请输入小字符串:china
控制台输出:小字符串china,在大字符串中共出现3次
public class Exam03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = "woaichina,chinabutongyubaima,wulunchinahaishijpan,zhaodaogongzuojiushihaoma";
String sub = "china";
int filter = filter(s, sub);
System.out.println(sub+"在大字符串中出现了"+filter+"次");
}
public static int filter(String s,String sub){
int old_length=s.length();
String replace="";
if (s.contains(sub)){
replace = s.replace(sub, "");//将需要查找的字符串替换为空
}
int new_length= replace.length();//用原来字符串的长度去减替换过的字符串就是该字符串中字符出现的次数
int count=(old_length-new_length)/(sub.length());//因为是字符串中字符出现的次数,所以要除以字符串你的长度最后就是字符串在另一个字