1

If I have a string line = "XYZTGEXGXRX", line.indexOf("X"); returns the index of the first "X" that is contained in that string.

What I want to know, is what what would allow me to get the second "X", or any occurrence of "X" after that?

VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
  • 1
    What language are you looking for a solution in? C#, Java, JavaScript... The answer would be different depending on choice of language. – VulgarBinary May 12 '15 at 05:21
  • Sorry about that, its Java! – Larissa Lemere May 12 '15 at 05:45
  • possible duplicate of [index of string in multiple positions](http://stackoverflow.com/questions/29595501/index-of-string-in-multiple-positions) –  May 12 '15 at 05:50

1 Answers1

2

Answer can be found here: Java indexOf method for multiple matches in String

There is a second parameter for indexOf which sets a start parameter. This code example prints all indices of x

i = str.indexOf('x');
while(i >= 0) {
     System.out.println(i);
     i = str.indexOf('x', i+1);
}
Community
  • 1
  • 1
davidgiga1993
  • 2,695
  • 18
  • 30