Java indexOf method for multiple matches in String
There's a another version of indexOf
method, taking fromIndex
as parameter.
So, you can call it in a loop, each time passing prevPosition + 1
as a second parameter.
Documentation:
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#indexOf(int, int)
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);
}