[Java] 纯文本查看 复制代码
import java.util.Scanner;
/*
* 1.键盘录入一个大字符串,再录入一个小字符串
2.统计小字符串在大字符串中出现的次数
3.代码运行打印格式:
请输入大字符串:woaiheima,heimabutongyubaima,wulunheimahaishibaima
,zhaodaogongzuojiushihaoma
请输入小字符串:heima
控制台输出:共出现3次
*
* */
public class Topic02 {
public static void main(String[] args) {
String bigString;
String smallString;
int count=0;
Scanner scanner=new Scanner(System.in);
System.out.println("请输入大字符串");
bigString=scanner.next();
System.out.println("请输入小字符串");
smallString=scanner.next();
//通过截取字符串
int index;
while(true){
index=bigString.indexOf(smallString);
count++;
bigString=bigString.substring(index+smallString.length());
if (!bigString.contains(smallString)||bigString.length()<smallString.length()){
break;
}
}
System.out.println("共出现"+count+"次");
}
}