Java indexOf method for multiple matches in String
Asked Answered
N

5

29

I had a question about the indexOf method. I want to find multiple cases of "X" in a string.

Suppose my string is "x is x is x is x", I want to find x in all of its index positions. But how do you do this for multiple cases? Is this even possible with indexOf?

I did int temp = str.indexOf('x'); It find the first x. I tried to do a for loop where i is initialized to length of string and this did not work since I kept finding the first x over and over.

for (int y = temp1; y >= 0;y-- ) 
{
    int temp = str.indexOf('x');
    System.out.println(temp);
}

But this does not work. Am I supposed to use regex? Because I don't really know how to use regex method.

Any help would be appreciated, thanks!

Newscast answered 14/2, 2011 at 1:5 Comment(0)
T
53

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);
}
Tomi answered 14/2, 2011 at 1:11 Comment(1)
Ah, thank you very much everyone. That makes a lot of sense since you just iterate through the string based on position of last match.Newscast
R
12

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)

Router answered 14/2, 2011 at 1:8 Comment(0)
A
5

You can specify the start index with indexOf. So, in your loop you store the last position of 'x', then search again using that index + 1.

Arvid answered 14/2, 2011 at 1:9 Comment(0)
A
3

Here is the solution with Streams API :

String text = "Lets search for E ?";

final char key = 'e';
int[] indices = IntStream.range(0, text.length())
    .filter(i -> text.charAt(i) == key)
    .toArray();

Output will be like this :

//1
//6

If you want to add upper case letters into your result, you need to change the filter's argument closure with :

.filter(i -> Character.toLowerCase(text.charAt(i)) == key)

Then the output will be like this :

//1
//6
//16

Additionally if you just want to use the results or print it out for quick testing purposes you can do it with using forEach terminal operation:

IntStream.range(0, text.length())
    .filter(i -> text.charAt(i) == key)
    .forEach(System.out::println);
Aeromedical answered 11/6, 2016 at 13:44 Comment(0)
C
0
String name = "alameer kaiser aziz";
String found = "a";
int num = name.indexOf(found);
while (num >=0) {
    System.out.println(num);
    num = name.indexOf(found,num+1);
}
Cuttler answered 13/7, 2017 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.