获取一个字符串中,另一个字符串出现的次数

* 思想:
* 1. indexOf到字符串中到第一次出现的索引
* 2. 找到的索引+被找字符串长度,截取字符串
* 3. 计数器++

package cn.dongWork;

public class homeWork {

public static void main(String[] args) {

String str1 = "heloloolooloow";
String str2 = "loo";
int count=0;
while (true) {
int index = str1.indexOf(str2);//0
if (index != -1) {
count++;
str1 = str1.substring(index+str2.length()); //每次截取不包含出现过的字符

} else {
break;
}
}

System.out.println(count);


}
}

原文地址:https://www.cnblogs.com/dongzhenkun/p/10554680.html