首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >子字符串在字符串中的出现次数

子字符串在字符串中的出现次数
EN

Stack Overflow用户
提问于 2009-04-20 18:47:52
回答 24查看 369.7K关注 0票数 140

为什么下面的算法对我来说没有停止?(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);
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/767759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档