字符串查找判断
输入两个字符串s1和s2,判断s1是否包含s2,给出结论,若包含,还需计算s1中s2的个数。要求使用string类型
import java.util.Scanner; public class Study { public static void main(String[] args) { String str1, str2, str3; int m, n; int count = 0; Scanner in = new Scanner(System.in); System.out.print("请输入字符串s1:"); str1 = in.nextLine(); System.out.print("请输入字符串s2:"); str2 = in.nextLine(); // 判断字符串s1是否包含s2 if ((str1.indexOf(str2)) != -1) { System.out.println("s1包含s2"); } else { System.out.println("s1不包含s2"); } m = str1.length(); n = str2.length(); // 判断s1中s2的个数 for (int i = 0; i < m - n + 1; i++) { str3 = str1.substring(i, i + n); if (str3.equals(str2)) { count++; } } System.out.println("包含个数为:" + count); } }