import java.util.Scanner;
public class String11 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入大串");
String big = sc.nextLine();
System.out.println("请输入你要查找的小串");
String small=sc.nextLine();
find(big,small);
}
public static void find(String big,String small){
int count=0;//定义统计变量
int index =big.indexOf(small);//获取小串第一次出现的索引
while(index!=-1){//判断是否有索引
count++;
big = big.substring(index+small.length());//截取第一次的索引加小串长度后再赋值给大串;
index =big.indexOf(small);
}System.out.println(small+"共出现"+count+"次");
}
}
|
|