java如何检查字符串是否包含搜索项
我有一个String
和一个String[]
填充了搜索项
如何检查我的String
是否包含所有搜索项
以下是一个例子:
案例1:
String text = "this is a stupid test";
String[] searchItems = new String[2];
searchItems[0] = "stupid";
searchItems[1] = "test";
案例2:
String text = "this is a stupid test";
String[] searchItems = new String[2];
searchItems[0] = "stupid";
searchItems[1] = "tes";
在案例1中,该方法应返回true
,但在案例2中,该方法应返回false
# 1 楼答案
您可以在正则表达式中使用word boundaries来实现这一点:
这些边界确保搜索词只有在整个词出现在文本中时才会匹配。因此,
"tes"
将不会与"test"
匹配,因为"\btes\b"
不是"\btest\b"
的子字符串# 2 楼答案
我会尝试用空格分开绳子,然后把所有的夹板部分都圈起来
为了让代码正常工作,可以这样做:
# 3 楼答案
注意:
"\\b"
将确保只匹配“整词”# 4 楼答案
# 5 楼答案
我会把课文中的所有单词排成一个数组。 然后用2个循环检查textArray是否包含所有搜索词