简体   繁体   English

Java,如果列表中的修剪字符串包含字符串,则返回

[英]Java, return if trimmed String in List contains String

In Java, I want to check whether a String exists in a List<String> myList .在 Java 中,我想检查List<String> myList是否存在List<String> myList

Something like this:像这样的东西:

if(myList.contains("A")){
    //true
}else{
    // false
}

The problem is myList can contain un-trimmed data:问题是 myList 可以包含未修剪的数据:

{'  A', 'B  ', '  C  '}

I want it to return true if my item 'B' is in the list.如果我的项目'B'在列表中,我希望它返回 true。 How should I do this?我该怎么做? I would like to avoid a looping structure.我想避免循环结构。

With Java 8 Stream API:使用 Java 8 Stream API:

List<String> myList = Arrays.asList("  A", "B  ", "  C  ");
return myList.stream().anyMatch(str -> str.trim().equals("B"));

You need to iterate your list and call String#trim for searching:您需要迭代您的列表并调用String#trim进行搜索:

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

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.当搜索词在开始/结束处包含开/关方括号时,我在每一端添加了[]以防止误报匹配。

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;

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您需要使用str.equalsstr.equalsIgnoreCase ,而不是contains因为containsstring的作品不一样containsList

List<String> s = Arrays.asList("BAB", "SAB", "DAS");
s.contains("A"); // false
"BAB".contains("A"); // true

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:如果您使用的是 java8,您可以使用单行代码简单地修剪字符串:

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.上一行的重要性在于,将来您也可以使用 List 或 set。 Now you can use your own code:现在您可以使用自己的代码:

if(myList.contains("A")){
    //true
}else{
    // false
}

您可以使用近似字符串匹配库来执行此操作,例如SecondString ,但这几乎肯定是矫枉过正——只需使用提供的 for-loop 答案之一即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM