在java中寻找特定的字符串模式

时间:2013-09-12 16:41:43

标签: java

我有一个要求,我必须在一个集合中添加一些字符串模式,只要它不存在,我需要注意确切的模式。

对于ex,假设对于给定的键,值为abc,bcd,aefg,并且要添加的新模式是efg然后应该添加(假设值用逗号分隔为分隔符)

我打算使用StringBuffer.indexOf(String Str)方法,但是我没有使用它,因为它会返回索引,如果要查找的模式作为值中的子字符串出现(在上面的例子中,efg已经作为子字符串出现) )。 我知道有一些解决方案使用Java 5的Regex功能,但我需要稍微介绍一下这个主题。 截至目前,我正在做以下

  1. 迭代我得到的一个键(由分隔符分隔的值列表)的值
  2. 为新模式(要添加)执行完全匹配,并仅在其不存在时添加。
  3. 有没有更好的解决方案?

4 个答案:

答案 0 :(得分:0)

除非我误解了你,否则contains方法应该有效?

str1.toLowerCase()。包含( “EFG”)

答案 1 :(得分:0)

你能用一套吗?

String[] strings = myString.split(",");
Set<String> set;
//add strings to set

如果不是:

String[] strings = myString.split(",");
List<String> list;
for (String s : strings) {
   if(!list.contains(s)) {
       list.add(s);
   }
}

答案 2 :(得分:0)

集合接口本身有一个contains方法。如果您只想添加一个集合是否还没有包含确切的字符串,那么您可以使用它并保存您自己迭代内容。集合contains方法将使用“equals”方法为其内容:

http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object)

答案 3 :(得分:0)

你可以尝试类似的东西:

@Test
public void addString() {
    String target = "abc,bcd,aefg";
    String stringToAdd = "efg";

    System.out.println(target);

    if(! new HashSet<String>(Arrays.asList(target.split(","))).contains(stringToAdd)) {
        target += "," + stringToAdd;
    }
    System.out.println(target);

    if(! new HashSet<String>(Arrays.asList(target.split(","))).contains(stringToAdd)) {
        target += "," + stringToAdd;
    }
    System.out.println(target);
}

<强>输出

abc,bcd,aefg
abc,bcd,aefg,efg
abc,bcd,aefg,efg

代码如下:

  1. split使用,
  2. converting the array to a list制作一组结果数组并将其提供给HasSet's contructor
  3. 使用Set的方法contains检查要添加的字符串是否存在
  4. 如果不存在则添加
  5. 修改

    您可以尝试使用此正则表达式^efg|^efg,.+|.+,efg,.+|.+,efg$

    @Test
    public void addStringRegEx() {
        String target = "abc,bcd,aefg";
        String stringToAdd = "efg";
    
        String regex =
                // string is alone at first position and there is no comma
                "^" + stringToAdd + 
                "|" +  // OR
                // string is first and is followed by a comma and some other strings
                "^" + stringToAdd + ",.+" +   
                "|" +   // OR
                // string is enclosed within two commas i.e. in the middle
                ".+," + stringToAdd + ",.+" +
                "|" +   // OR
                // string is at the end and there are some strings and comma before it
                ".+," + stringToAdd + "$";
    
        Assert.assertFalse("aefg".matches(regex));
        Assert.assertFalse(",efg".matches(regex));
        Assert.assertFalse("efg,".matches(regex));
        Assert.assertFalse(",efg,".matches(regex));
    
        Assert.assertTrue("efg".matches(regex));
        Assert.assertTrue("foo,efg".matches(regex));
        Assert.assertTrue("foo,efg,bar".matches(regex));
        Assert.assertTrue("efg,bar".matches(regex));
    
    
        if(! target.matches(regex)) {
            target += "," + stringToAdd;
        }
        Assert.assertEquals("abc,bcd,aefg,efg", target);
    
        if(! target.matches(regex)) {
            target += "," + stringToAdd;
        }
        Assert.assertEquals("abc,bcd,aefg,efg", target);
    }