//判断字符串s中有多少s1,
//s=aaa,s1=aa,则输出1,而不是2
public static int countK2(String s, String s1) {
if (s == null || s1 == null) {
return 0;
}
int count = 0;
int len = s1.length();
for (int i = 0; i < s.length();i++ ) {
if (i + len > s.length()) {//超过最大长度了
return count;
} else {//佛则继续比较
if (s.charAt(i) == s1.charAt(0)) {
for (int j = 1; j < s1.length(); j++) {
if (s.charAt(i + j) != s1.charAt(j)) {//不等,下一个i++
break;
}
if (j == s1.length() - 1) {//找到了,直接跳过这个s1长度
count++;
i=i+len-1;//本来直接i=i+len开始遍历,但是外层for循环i++了,所以要i=i+len-1;
}
}
}
}
}
return count;
}
/*
//判断字符串s中有多少s1,如aaa 有 3个a,
public static int countK(String s, String s1) {
if (s == null || s1 == null) {
return 0;
}
int count = 0;
int len = s1.length();
for (int i = 0; i < s.length(); i++) {
if (i + len > s.length()) {//超过最大长度了
return count;
} else {//佛则继续比较
if (s.charAt(i) == s1.charAt(0)) {
for (int j = 1; j < s1.length(); j++) {
if (s.charAt(i + j) != s1.charAt(j)) {
break;
}
if (j == s1.length() - 1) {
count++;
}
}
}
}
}
return count;
}*/
判断字符串s中有多少s1
最新推荐文章于 2023-03-30 17:00:38 发布