Java indexOf method for multiple matches in String

There is a second variant of the indexOf method, which takes a start-index as a parameter.

i = str.indexOf('x');
while(i >= 0) {
     System.out.println(i);
     i = str.indexOf('x', i+1);
}

Leave a Comment