为什么下面的算法对我来说没有停止?(str是我要搜索的字符串,findStr是我要查找的字符串)
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr,lastIndex);
if( lastIndex != -1)
count++;
lastIndex += findStr.length();
}
System.out.println(count);
https://stackoverflow.com/questions/767759
复制相似问题