包含String的方法无法正常工作
问题描述:
我有一个问题,但我无法解决这个问题。
I have one question but I am not able to solve this .
public static void main(String [] arg) {
String description = "This time only $FB is highest priority";
List<String> list = new ArrayList<String>();
list.add("$FB");
list.add("$F");
for(String s : list) {
if(description.contains(s)) {
System.out.println(s);
}
}
}
我得到的输出是$ FB和$ F,但是这个虚拟字符串只包含列表中的一个字符串..还有其他任何方法只能提供完全匹配吗?
The out put I am getting is $FB and $F but this dummy string contains only one string of the list .. Is any other method to do which will give only exact match ?
答
您可以使用正则表达式检查字符串是否包含单词:
You can use a regular expression to check if a string contains a word:
if (description.matches(".*\\b"+Pattern.quote(s)+"\\b.*")) {
...
}
锚点 \ b
匹配字边界。