【发布时间】:2020-07-06 03:55:21
【问题描述】:
我有这样一句话:
I`ve got a Pc
还有一组词:
Hello
world
Pc
dog
如何检查句子中是否包含这些单词?在此示例中,我将匹配 Pc。
这是我目前得到的:
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!");
}
}
}
【问题讨论】:
-
您只需迭代数组中的所有元素并使用
line.contains(arrayElement)。如果有任何匹配项,您就有匹配项。
标签: java arrays string if-statement contains