str.contains in Java

时间:2014-08-06 06:00:50

标签: java string string-matching contain

我有2个名单

allWords [book, pen, pencil] 
subsetString [book  pen  , book  pencil  , pen  pencil  ]

我希望我的输出为

book  pen => pencil
book  pencil => pen
pen  pencil => book

即对于subsetString中的每个元素,我将用allwords检查它。 一旦找不到匹配,所有字的字符串将被添加到输出的RHS

但我的问题是 现在我只得到2输出而不是3

allWords [book, pen, pencil]
subsetString [book  pen  , book  pencil  , pen  pencil  ]
pen  pencil   => book
book  pen   => pencil

原因是在进入book pencil时,会使用allWords [book, pen, pencil]

进行检查

一旦涉及笔包含book pencil - 它就越来越令人满意(因为pencil包含子字符串pen)。

CODE

public void getStrongRules2(ArrayList<String> allWords,String delim) {
        ArrayList<ArrayList<String>> subsets = BTSum(allWords);
        ArrayList<String> subsetString = new ArrayList<String>();

        for (int j = 0; j < subsets.size(); j++) {
            String substring = "";
            for (int k = 0; k < subsets.get(j).size(); k++) {

                substring += subsets.get(j).get(k) + "  ";
            }
            subsetString.add(substring);
        }
        System.out.println("allWords "+allWords);
        System.out.println("subsetString "+subsetString);

        for(String a : allWords){
            for (int j = 0; j < subsetString.size(); j++) {
                if (!(subsetString.get(j).contains(a))) {
                    System.out.println(subsetString.get(j)+" => "+a);
                }
            }
        }
    }
    public static ArrayList<ArrayList<String>> BTSum(ArrayList<String> numbers) {

        int n = numbers.size();
        ArrayList<ArrayList<String>> powerSet = new ArrayList<ArrayList<String>>();

        for (long i = 0; i < (1 << n); i++) {
            ArrayList<String> element = new ArrayList<String>();
            for (int j = 0; j < n; j++)
                if ((i >> j) % 2 == 1) {
                    element.add(numbers.get(j));
                }
            if (element.size() > 1 && element.size() < n) {
                powerSet.add(element);
            }
        }
        return powerSet;
    }

}

但这不应该发生在我的情况下。

如何纠正。

请建议

7 个答案:

答案 0 :(得分:1)

考虑

     List<String> allWords = new ArrayList<>();
     allWords.add("Book"); allWords.add("Pen"); allWords.add("Pencil") ; 

如果您能够将子集拆分为三个不同的列表而不是单个arrayList,请执行拆分

     list1 [Book,Pen] 
     list2 [Book,Pencil]
     list3 [Pen,Pencil]

您可以在不执行循环的情况下执行此操作

     Set<String> results1 = new HashSet<String>(allWords);
     results1.removeAll(list1);
     System.out.println("result1 : "+ list1 +" ===> "+ results1);

     Set<String> results2 = new HashSet<String>(allWords);
     results2.removeAll(list2);
     System.out.println("result2 : "+ list2 +" ===> "+ results2);

     Set<String> results3 = new HashSet<String>(allWords);
     results3.removeAll(list3);
     System.out.println("result3 : "+ list3 +" ===> "+ results3);

输出

result1 : [Book, Pen] ===> [Pencil]
result2 : [Book, Pencil] ===> [Pen]
result3 : [Pen, Pencil] ===> [Book]

希望这有帮助

答案 1 :(得分:0)

只需按照desc的长度对子字符串进行排序,然后查找它们。 使用自定义编译器,如:

public class MyComparator implements java.util.Comparator<String> {

    private int referenceLength;

    public MyComparator(String reference) {
        super();
        this.referenceLength = reference.length();
    }

    public int compare(String s1, String s2) {
        int dist1 = Math.abs(s1.length() - referenceLength);
        int dist2 = Math.abs(s2.length() - referenceLength);

        return dist1 - dist2;
    }
}

按字母排序关键字的子串数组

java.util.Collections.sort(List, myComparator )

答案 2 :(得分:0)

这是因为(subsetString.get(j).contains(a)) - &gt;&gt; “钢笔铅笔”.contains('pen')是真的。因此,它不会进入你的if循环打印结果。

答案 3 :(得分:0)

问题是book pencil'pen'cil包含pen因此您无法使用contains()获得正确的结果。

"book pencil".contains("pen") ===> true

所以你可以尝试类似下面的内容

 ArrayList<String> allWords = new ArrayList<String>() {{
        add("book");
        add("pen");
        add("pencil");
    }};
 ArrayList<String> subsetString = new ArrayList<String>() {{
        add("book  pen");
        add("book  pencil");
        add("pen  pencil");
    }};

 for (String i : subsetString) {
      List<String> stringList = Arrays.asList(i.split(" "));
        for (String j : allWords) {
            if(!stringList.contains(j)) {
               System.out.println(i + " => " + j);
            }
        }
  }  

答案 4 :(得分:0)

您不需要所有代码。只需使用List.removeAll()来完成所有繁重的工作:

List<String> allWords = Arrays.asList("book", "pen", "pencil");
List<List<String>> subsetString = Arrays.asList(
    Arrays.asList("book", "pen"), 
    Arrays.asList("book", "pencil"),
    Arrays.asList("pen", "pencil"));

for (List<String> subset : subsetString) {
    ArrayList<String> strings = new ArrayList<String>(allWords);
    strings.removeAll(subset);
    System.out.println(subset + " => " + strings);
}

输出:

[book, pen] => [pencil]
[book, pencil] => [pen]
[pen, pencil] => [book]

答案 5 :(得分:0)

您可以这样做:

    List<String> allWords = new ArrayList<String>();
    allWords.add("book");
    allWords.add("pen");
    allWords.add("pencil");

    List<String> subsetStrring = new ArrayList<String>();
    subsetStrring.add("book pen");
    subsetStrring.add("book pencil");
    subsetStrring.add("pen pencil");

    for (String string : subsetStrring) {
        List<String> subsetStr = Arrays.asList(string.split(" "));
        for (String word : allWords) {
            if(!subsetStr.contains(word)) {
                System.out.println(string + " => " + word);
                break;
            }
        }
    }

答案 6 :(得分:0)

我认为使用equals匹配字符串可以避免subString问题

for(String a : allWords)
{
    for (int j = 0; j < subsets.size(); j++) 
    {
        boolean isMatch = false;
        String subSetString = "";
        for(int k = 0 ; k < subsets.get(j).size() ; k++)
        {
            if(subsets.get(j).get(k).equals(a))
            {
                isMatch = true;
                break;
            }
            subSetString += subsets.get(j).get(k) + " ";
        }
        if(!isMatch) 
        {
            System.out.println(subSetString + " => " + a);
        }
    }
}