Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find all first indexes in the string?

Tags:

java

I am 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 find only the first index, how to find all first indexes in the string ? (in this case three index)

like image 510
Max Usanin Avatar asked Dec 04 '22 00:12

Max Usanin


1 Answers

After you've found the first index, use the overloaded version of indexOf that receives the start index as a second parameter:

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.

Keep doing that until indexOf returns -1, indicating that there are no more matches to be found.

like image 149
Óscar López Avatar answered Dec 15 '22 21:12

Óscar López