我想要find all the substring
一个字符串contains a key word
。
例如:“这是keyword
字符串中的”。
输出:关键字,这是关键字,字符串中的关键字,...中的关键字。
我想先找到所有子字符串,然后尝试一个一个过滤。但我认为这将是一个非常糟糕的解决方案。
你能给我一些建议吗?非常感谢。
我已经编辑只是找到标记的序列。
Try this:
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);
Output for the code above:
[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...]