查找所有子字符串包含关键字

时间:2013-08-21 18:49:42

标签: java substring

我想要find all the substring一个字符串contains a key word

Ex:"这是字符串"中的keyword

输出:关键字,这是关键字,字符串中的关键字是....中的关键字

我想先找到所有子串然后尝试逐个过滤。但我认为这将是非常糟糕的解决方案。

请你给我一些建议!非常感谢你。

我已经编辑过以找到令牌序列。

2 个答案:

答案 0 :(得分:2)

试试这个:

String str = "abcdefkeybncv...";
String key = "key";
int index = str.indexOf(key);
ArrayList<String> sub = new ArrayList<String>();
for (int i = 0; i < str.length(); i++) {
    for (int j = 0; j <= str.length() - i; j++) {
        String s = str.substring(i, i+j);
        if(s.indexOf(key) >= 0){
            sub.add(s);
        }
    }
}
System.out.println(sub);

上述代码的输出:

[abcdefkey, abcdefkeyb, abcdefkeybn, abcdefkeybnc, abcdefkeybncv, abcdefkeybncv., abcdefkeybncv.., abcdefkeybncv..., bcdefkey, bcdefkeyb, bcdefkeybn, bcdefkeybnc, bcdefkeybncv, bcdefkeybncv., bcdefkeybncv.., bcdefkeybncv..., cdefkey, cdefkeyb, cdefkeybn, cdefkeybnc, cdefkeybncv, cdefkeybncv., cdefkeybncv.., cdefkeybncv..., defkey, defkeyb, defkeybn, defkeybnc, defkeybncv, defkeybncv., defkeybncv.., defkeybncv..., efkey, efkeyb, efkeybn, efkeybnc, efkeybncv, efkeybncv., efkeybncv.., efkeybncv..., fkey, fkeyb, fkeybn, fkeybnc, fkeybncv, fkeybncv., fkeybncv.., fkeybncv..., key, keyb, keybn, keybnc, keybncv, keybncv., keybncv.., keybncv...]

答案 1 :(得分:0)

  1. 构建后缀数组:http://en.wikipedia.org/wiki/Suffix_array
  2. 使用二进制搜索在那里找到你的子字符串
  3. 从后缀数组中的此点向上和向下移动,而后缀以substring
  4. 开头