简体   繁体   English

检查字符串是否包含列表中的任何字符串

[英]Check if a string contains any of the strings from an List

I am quite new to java and currently stuck and do not know how to proceed.我对 java 很陌生,目前卡住了,不知道如何继续。

What I want to do is to check if a string contains any words from a list of words and if yes output them.我想要做的是检查一个字符串是否包含单词列表中的任何单词,如果是 output 它们。

In my case all strings will have a similar text like this one (example with 5 mins):在我的情况下,所有字符串都将具有类似的文本(例如 5 分钟):

Set timer to five minutes

or this one:或者这个:

Timer five minutes

This is my current code with some comments what I am trying to do:这是我当前的代码,带有一些我想要做的评论:

import java.util.stream.Stream; 

class GFG { 

// Driver code 
public static void main(String[] args) 
{ 

String example = Set timer to five minutes

    Stream<String> stream = Stream.of(("Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten") //The stream/array would be much bigger since I want to cover every number till 200 

//Now I thought I can use stream filter to check if example contains the word Timer and any of the words of the Stream and if it does I want to use the output to trigger something else

    if(stream.filter(example -> example .contains(NOTSUREWHATTOPUTHERE))) {
       //If detected that String contains Timer and Number, then create timer 
    } 
} 

Can anyone give me some advice/help?谁能给我一些建议/帮助?

regards问候

You can do it like this:你可以这样做:

String[] words = { "Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };

String example = "Set timer to five minutes";

String exLower = example.toLowerCase();
if (Stream.of(words).anyMatch(word -> exLower.contains(word.toLowerCase()))) {
    //
}

That code will at least check correctly, even when words have different upper-/lower-case, but it fails if the text has a word embedded in another word, eg text "stone" will match because "one" is found.该代码至少会正确检查,即使单词具有不同的大写/小写,但如果文本中嵌入了另一个单词,则它会失败,例如文本"stone"将匹配,因为找到"one"

To fix that, the "easiest" would be to convert the word list to a regex.要解决这个问题,“最简单”的方法是将单词列表转换为正则表达式。

String[] words = { "Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };

String example = "Set timer to five minutes";

String regex = Stream.of(words).map(Pattern::quote)
        .collect(Collectors.joining("|", "(?i)\\b(?:", ")\\b"));
if (Pattern.compile(regex).matcher(example).find()) {
    //
}

The correct one would be正确的应该是

String example = "Set timer to five minutes";
Stream<String> stream = Stream.of("Timer", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten");
if (stream.anyMatch(something -> example.contains(something))) {

}

You should give the lambda variable a different name other than example because that conflicts with the already existing local variable.您应该为 lambda 变量指定一个不同于example的名称,因为这与已经存在的局部变量冲突。
Additionally you can / should use anyMatch instead of filter + checking the length.此外,您可以/应该使用anyMatch而不是filter + 检查长度。

Or just:要不就:

if (stream.anyMatch(example::contains)) {

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

相关问题 HQL检查字段是否包含字符串列表中的任何字符串 - HQL to check if a field CONTAINS any of the String from a list of String 如何检查列表中的所有项目是否包含字符串中的任何字符 - How to check that all items in the list contains any character from string 测试字符串是否包含数组中的任何字符串 - Test if a string contains any of the strings from an array 检查字符串是否包含数组中的所有字符串 - Check if string contains all strings from array 如何检查ArrayList <String> 包含字符串数组中的任何元素? - How can I check if an ArrayList<String> contains any elements from an array of Strings? 检查列表是否包含来自字符串列表的 substring 而不迭代列表 - Check if a list contains an substring from a list of strings without iterating the list 如何验证String是否包含List中的任何字符串? - How can I validate whether the String contains any of Strings from a List? 如何检查字符串列表中的特定单词是否包含在字符串中,但不应该在任何其他单词之间? - How to check if a particular word from a string list contains in a string, but it should not be between any other words? 检查字符串包含数组列表中的属性 - Check string contains attribute from array list 检查字符串是否包含列表中的元素 - Check if a String contains an element from a List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM