/*需求:比对两个字符串str1和str2,看str2中所有字母是不是都包含再str1中,(str1比较长)*/ |
/*需求:比对两个字符串str1和str2,看str2中所有字母是不是都包含再str1中,(str1比较长)*/ package 字符串; public class IncludeString { public static void main(String[] args) { String str1 = "abc"; String str2 = "ac"; System.out.println(IncludeString.includeString(str1, str2)); } public static boolean includeString(String str1, String str2) { // 由于字符串不好操作,就把字符串转化为字符数组 char[] ch1 = str1.toCharArray(); char[] ch2 = str2.toCharArray(); // 接下来使用两用两个for循环来嵌套比对, int n = 0; int i; for (i = 0; i < ch2.length; i++) { for (int j = 0; j < ch1.length; j++) { // 如果查到和当前字母相等的,就退出本次循环进入到外边一个循环 if (ch2[i] == ch1[j]) { n++; break; } } } if (i == n) { return true; } else { return false; } } }
控制台输出如下: