计算一个字符串在另一个字符串中出现的次数

public class Test3 {

public static void main(String[] args) {
String s1 = "ab";
String s2 = "abdkjfabdgjab";
System.out.println(count(s1, s2));
}

public static int count(String s1, String s2) {
int count = 0;
while (s2.indexOf(s1) != -1) {
int begin = s2.indexOf(s1) + s1.length();
s2 = s2.substring(begin);
count++;
}
return count;
}
}

posted @ 2020-07-03 19:00  一口獠牙  阅读(249)  评论(0编辑  收藏  举报