检查字符串是否包含数组中的所有字符串
问题描述:
如何检查 String
是否包含来自 Array $ c $的所有
String
c>。
How to check if String
contains all Strings
from Array
.
我当前的代码:
String word = "abc";
String[] keywords = {"a", "d"};
for(int i = 0; i < keywords.length; i++){
if(word.contains(keywords[i])){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
答
如果将其包装到单独的方法中,代码将看起来更好:
The code would look much more nicer if you wrap it into a separate method:
public static boolean containsAllWords(String word, String ...keywords) {
for (String k : keywords)
if (!word.contains(k)) return false;
return true;
}