相关疑难解决方法(0)

字符串中子字符串的出现

为什么以下算法不会停止?(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);
Run Code Online (Sandbox Code Playgroud)

java string

117
推荐指数
11
解决办法
30万
查看次数

标签 统计

java ×1

string ×1