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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16218863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:51:23  来源:igfitidea点击:

Java, return if trimmed String in List contains String

javaarraysstringlist

提问by Reddy

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

在 Java 中,我想检查一个字符串是否存在于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. How should I do this? I would like to avoid a looping structure.

如果我的项目'B'在列表中,我希望它返回 true 。我该怎么做?我想避免循环结构。

采纳答案by anubhava

You need to iterate your list and call String#trimfor 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))

回答by Reddy

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;

回答by Zim-Zam O'Pootertoot

You may be able to use an approximate string matching library to do this, e.g. SecondString, but that is almost certainly overkill - just use one of the for-loop answers provided instead.

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

回答by Bohemian

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.

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

回答by nkukhar

Try this:

尝试这个:

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

You need to use str.equalsor str.equalsIgnoreCaseinstead of containsbecause containsin stringworks not the same as containsin List

您需要使用str.equalsstr.equalsIgnoreCase代替,contains因为containsstring作品中与containsList

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

回答by rogermenezes

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

回答by vikash

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. Now you can use your own code:

上面这行的重要性在于,将来您也可以使用 List 或 set。现在您可以使用自己的代码:

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