public class Test {
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();
int count = 0;
String temp = big; //temp,big都指向常量池中的同一个字符串
while(temp.indexOf(small) != -1) {
temp = temp.substring(temp.indexOf(small) + small.length()); //temp的地址值随着新字符串的生成,被新字符串的地址值覆盖,而big中还是记录着原字符串的地址值
count++;
}
System.out.println(count);
}
}
|
|