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.
matchestries to match the entire string. If you want to usematchesyou would have to use"^" + q + ".*"or something similar. (You would probably want to wrapqinPattern.quoteas well.)temp = "Two.*"then1should be printed. Isn't it? (String.indexOfcan only be used on aString, not a list of strings.)