Java – how do I find all the first indexes in a string?

I use this source:

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

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

But I found that there is only the first index. How to find all the first indexes in the string? (in this case, three indexes)

Solution

After finding the first index, use the overloaded version indexof that receives the starting index as the second parameter:

Continue to do this until indexof returns - 1, indicating that there are no more matches to find

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>