关键字查找
一句话:这是一棵苹果树。
查询是否在关键词:香蕉、苹果、橘子里面
/**
* 翼声关键字查找处理数据
*/
@Test
public void containsWords() {
String remark = "这是一棵苹果树";
List<String> yyyyAll = Arrays.asList("苹果", "香蕉", "橘子");
boolean isKeyword = containsWords(remark, yyyyAll);
if (isKeyword) {
System.out.println("该语句存在关键字:" + remark);
} else {
System.out.println("该语句不存在存在关键字:" + remark);
}
}
/**
* 关键字查找
*
* @param inputString
* @param itemList
* @return
*/
public static boolean containsWords(String inputString, List<String> itemList) {
//String[] items1 = new String[itemList.size()];//异常出现All elements are null
String[] items = itemList.toArray(new String[itemList.size()]);
return containsWords(inputString, items);
}
/**
* 关键字查找
*
* @param inputString
* @param items
* @return
*/
public static boolean containsWords(String inputString, String[] items) {
if (StringUtils.isEmpty(inputString) || items == null) {
return false;
}
boolean found = false;
for (String item : items) {
if (StringUtil.isEmpty(item)) {
continue;
}
if (inputString.contains(item.trim())) {
found = true;
break;
}
}
return found;
}