Java, return if trimmed String in List contains String

arrays, java, list, string

Solution

You need to iterate your list and call `String#trim` for searching:

String search = "A";
for(String str: myList) {
    if(str.trim().contains(search))
       return true;
}
return false;

OR if you want to perform ignore case search, then use:

search = search.toLowerCase(); // outside loop

// inside the loop
if(str.trim().toLowerCase().contains(search))

Problem

In Java, I want to check whether a String exists in a `List<String> myList`. Something like this: ``` if(myList.contains("A")){ //true }else{ // false } ``` The problem is myList can contain un-trimmed data: ``` {' A', 'B ', ' C '} ``` I want it to return true if my item `'B'` is in the list. How should I do this? I would like to avoid a looping structure.

Original source