加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何查找字符串中的所有第一个索引?

发布时间:2020-12-15 05:10:37 所属栏目:Java 来源:网络整理
导读:我使用这个来源: String fulltext = "I would like to create a book reader have create,create ";String subtext = "create";int i = fulltext.indexOf(subtext); 但我发现只有第一个索引,如何查找字符串中的所有第一个索引? (在这种情况下三个索引) 解
我使用这个来源:

String fulltext = "I would like to create a book reader  have create,create ";

String subtext = "create";
int i = fulltext.indexOf(subtext);

但我发现只有第一个索引,如何查找字符串中的所有第一个索引? (在这种情况下三个索引)

解决方法

找到第一个索引后,使用接收起始索引的重载版本 indexOf作为第二个参数:

public int indexOf(int ch,int fromIndex) Returns the index within this string of the first occurrence of the specified character,starting the search at the specified index.

继续这样做,直到indexOf返回-1,表示没有更多匹配要找到.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读