public class Demo3{ public static void main(String [] args){ int num = 0; int t = 0; Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); //存储键盘录入的字符串 String n = sc.next(); System.out.println("请输入需要查找的字符:"); String s = sc.next(); String [] Str = n.split(s); System.out.println(Str.length-1); } }
public class Demo3 { public static void main(String[] args) { int num = 0; int t = 0; Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); //存储键盘录入的字符串 String n = sc.next(); System.out.println("请输入需要查找的字符:"); String s = sc.next(); for(int i=0;i<n.length();i++){ t = n.indexOf(s,i); if(t!=-1){ num++; i=t; } } System.out.println(num); } }