包含String的方法无法正常工作 [英] contains method of String is not working properly

查看:78
本文介绍了包含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 匹配字边界。

这篇关于包含String的方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆