import java.util.Scanner;
/*
统计一个子串在整串中出现的次数
例如:整串:Hi,how are you?I'm fine,thank you,and you?
子串:you
分析:
1.输入整串
2.输入子串
3.循环操作
1)查找(如果==-1,break)
2)计数并截取字符串
*/
public class Task1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入整串:");
String s1=sc.nextLine();//next();遇到空格,回车。Tab表示输入结束
System.out.println("请输入子串:");
String s2=sc.next();
int index=0;//判断是否存在子串
int count=0;//计数
while(true)
{
index=s1.indexOf(s2);
if(index!=-1)
{
count++;
s1=s1.substring(index+s2.length());//把第一个子串截出
}
else
{
break;
}
}
System.out.println("子串出现的次数为:"+count);
}
}