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!");
}
}
}
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...