package demo1;
/**
*
*@author 高硕
*计算子串出现的次数
*1.indexof(String str,int fromIndex)返回指定字符串在此字符串中第一次出现处的索引
*2.计数count++
*3.fromIndex加上字符串的长度,寻找下处的位置
*4.停止条件,indexof返回-1
*/
import java.io.*;
import java.util.Scanner;
public class Main{
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//第一个
Scanner in = new Scanner(System.in);// 调用键盘输入API
String str =in.nextLine();//定义String 类型的变量str接住用户输入的字符串
String substr =in.nextLine();//定义String 类型的变量substr接住用户输入的字符串
findCount(str,substr);
}
public static void findCount(String src, String des) {
int index = 0;
int count = 0;
//indexof(String str,int fromIndex)返回指定字符串在此字符串中第一次出现处的索引
while ((index = src.indexOf(des, index)) != -1) {
count++;
index = index + des.length();
}
System.out.println(des + "出现了 " + count + " 次");
}
}
