Java, return if trimmed String in List contains String

后端 未结 7 2359
旧巷少年郎
旧巷少年郎 2020-12-04 17:52

In Java, I want to check whether a String exists in a List myList.

Something like this:

if(myList.contains(\"A\")){
    //         


        
7条回答
  •  死守一世寂寞
    2020-12-04 17:59

    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))
    

提交回复
热议问题