Check whether sentence contains certain words

Marco :

I have a sentence like:

I`ve got a Pc

And a set of words:

Hello
world
Pc
dog

How can I check whether the sentence contains any of those words? In this example I would have a match with Pc.

Here is what I got so far:

public class SentenceWordExample {
    public static void main(String[] args) {
        String sentence = "I`ve got a Pc";
        String[] words = { "Hello", "world", "Pc", "dog" };

       // I know this does not work, but how to continue from here?
       if (line.contains(words) {
            System.out.println("Match!");
       } else {
            System.out.println("No match!");
        }
    }
}
Mureinik :

I'd stream the array, and then check if the string contains any of its elements:

if (Arrays.stream(stringArray).anyMatch(s -> line.contains(s)) {
    // Do something...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360315&siteId=1