Java, return if trimmed String in List contains String

后端 未结 7 2332
旧巷少年郎
旧巷少年郎 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:55

    With Java 8 Stream API:

    List<String> myList = Arrays.asList("  A", "B  ", "  C  ");
    return myList.stream().anyMatch(str -> str.trim().equals("B"));
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
  • 2020-12-04 18:00
    String search = "A";
    for(String s : myList)
        if(s.contains(search)) return true;
    return false;
    

    This will iterate over each string in the list, and check if it contains the string you're looking for. If it's only spaces you want to trap for, you can do this:

    String search = "A";
    for(String s : myList)
        if(s.replaceAll(" ","").contains(search)) return true;
    return false;
    

    which will first replace spaces with empty strings before searching. Additionally, if you just want to trim the string first, you can do:

    String search = "A";
    for(String s : myList)
        if(s.trim().contains(search)) return true;
    return false;
    
    0 讨论(0)
  • 2020-12-04 18:02

    You can do it in a single line by using regex:

    if (myList.toString().matches(".*\\bA\\b.*"))
    

    This code should perform quite well.


    BTW, you could build the regex from a variable, like this:

    .matches("\\[.*\\b" + word + "\\b.*]")
    

    I added [ and ] to each end to prevent a false positive match when the search term contains an open/close square bracket at the start/end.

    0 讨论(0)
  • 2020-12-04 18:03

    You can use your own code. You don't need to use the looping structure, if you don't want to use the looping structure as you said above. Only you have to focus to remove space or trim the String of the list.

    If you are using java8 you can simply trim the String using the single line of the code:

    myList = myList.stream().map(String :: trim).collect(Collectors.toList());
    

    The importance of the above line is, in the future, you can use a List or set as well. Now you can use your own code:

    if(myList.contains("A")){
        //true
    }else{
        // false
    }
    
    0 讨论(0)
  • 2020-12-04 18:11

    Try this:

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

    You need to use str.equals or str.equalsIgnoreCase instead of contains because contains in string works not the same as contains in List

    List<String> s = Arrays.asList("BAB", "SAB", "DAS");
    s.contains("A"); // false
    "BAB".contains("A"); // true
    
    0 讨论(0)
提交回复
热议问题