Find substring in List and return Index Number

Tags: , , ,



I have an List, with Strings like:

String One
String Two
String Three

Now i would like to get the Index Number for the Substring “One”. How can i get this? I only could make it if i convert it to an Array and then:

public static int findInArray(Object[] arr, String searchName) {
int i;
for (i=0; i<arr.length; i++ ) { 
    String test = arr[i].toString();
    if (test.contains(searchName)) {
        //System.out.println("Substring found in:"+i);
        break;
    }
}
return i;
}

Isn’t there a way to search for the Substring without converting it to an Array?

Answer

List already contains indexes

 for(int i=0;i<list.size();++i){
     if (list.get(i).contains("searchString")) {
         System.out.println("Substring found in:"+i);
     }
 }


Source: stackoverflow