4

I need to get the index of an element in array to be searched:

 String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
 String q = "Two";  //need to find index of element starting with sub-sting "Two"

what I tried

Try-1

    String temp = "^"+q;    
    System.out.println(Arrays.asList(items).indexOf(temp));

Try-2

items[i].matches(temp)
for(int i=0;i<items.length;i++) {
    if(items[i].matches(temp)) System.out.println(i);
}

Both are not working as expected.

5
  • 1
    matches tries to match the entire string. If you want to use matches you would have to use "^" + q + ".*" or something similar. (You would probably want to wrap q in Pattern.quote as well.) Commented Sep 8, 2015 at 8:42
  • Thankyou its working for items[i].matches but not working for .indexof Commented Sep 8, 2015 at 8:47
  • What do you mean? If you use temp = "Two.*" then 1 should be printed. Isn't it? (String.indexOf can only be used on a String, not a list of strings.) Commented Sep 8, 2015 at 8:57
  • Wait, why don't you use a multi-dimensional array? Commented Sep 8, 2015 at 9:03
  • @aioobe Thanks great info Commented Sep 8, 2015 at 13:28

3 Answers 3

7

You would be better off using startsWith(String prefix) like this:

String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two";  //need to find index of element starting with substring "Two"
for (int i = 0; i < items.length; i++) {
    if (items[i].startsWith(q)) {
        System.out.println(i);
    }
}

Your first try does not work because you are trying to get the index of the String ^Two inside your list, but indexOf(String str) does not accept regular expression.

Your second try does not work because matches(String regex) works on the entire String, not just on the beginning.

If you are using Java 8, you could write the following code which returns the index of the first item beginning with "Two", or -1 if none was found.

String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q = "Two";
int index = IntStream.range(0, items.length).filter(i -> items[i].startsWith(q)).findFirst().orElse(-1);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Can we get index without loop ??
@Ravichandra To get the index, you are going to have to loop. If you are using Java 8, this can be hidden inside a Stream.
0

I think you will need to implement LinearSearch for this but with a twist, you were searching for substring. You can try this.

String[] items = {"One:10.1.22.33", "Two:10.1.21.23", "Three:10.1.21.33", "Four:10.1.21.23", "Five:10.1.22.23"};
String q= "Two";  //need to find index of element starting with sub-sting "Two"

for (int i = 0; 0 < items.length; i++) {
    if (items[i].startsWith(q)){
        // item found
        break;
    } else if (i == items.length) {
        // item not found
    }
}

Comments

-1
String q= "Five";String pattern = q+"(.*)";
for(int i=0;i<items.length;i++)
{
if(items[i].matches(pattern))
 { 
  System.out.println(i);
 }
}

2 Comments

For try-2 you should use q +"(.*)" . It will provide the index of q with any character
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.