【问题标题】:HashMap search and match regexHashMap 搜索和匹配正则表达式
【发布时间】:2012-01-17 00:54:22
【问题描述】:

我正在尝试在 HashMap 上搜索并将特定字符串与代表字符串的节点匹配。

为了完全匹配特定的值,应该这样做:

       for (Entry<String, Component> entry : hashMap.entrySet()) {
            if(entry.getValue().toString().matches("test1")){
                System.out.println(entry.getValue().toString());
            }
            else
            {
                System.out.println("Nothing found");
            }
        }

但我有不同的情况。该节点包含长字符串"xxx .. test 1 .."

那么,如何将“test 1”与这些节点字符串匹配?

【问题讨论】:

    标签: java regex search hashmap match


    【解决方案1】:

    你可以只使用 indexOf:

    if ( entry.getValue().toString().indexOf("test 1") != -1 ) {
    

    【讨论】:

      【解决方案2】:

      如果您只需要查看test 1 是否在字符串中,您可以使用String.contains() 方法。

      if(entry.getValue().toString().contains("test1")){
         System.out.println(entry.getValue().toString());
      }
      else
      {
         System.out.println("Nothing found");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-14
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        相关资源
        最近更新 更多