java 在Java中查找字符串中所有出现的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32788407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find all occurrences of substring in string in Java
提问by Kevin
I'm trying to find all occurrences of a substring in a string in Java.
我试图在 Java 的字符串中查找所有出现的子字符串。
For example: searching "ababsdfasdfhelloasdf" for "asdf" would return [8,17] since there are 2 "asdf"'s, one at position 8 and one at 17. Searching "aaaaaa" for "aa" would return [0,1,2,3,4] because there is an "aa" at positions 0,1,2,3, and 4.
例如:在“abasdfasdfhelloasdf”中搜索“asdf”将返回 [8,17],因为有 2 个“asdf”,一个在位置 8,一个在位置 17。搜索“aaaaaa”中的“aa”将返回 [0, 1,2,3,4] 因为在位置 0、1、2、3 和 4 处有一个“aa”。
I tried this:
我试过这个:
public List<Integer> findSubstrings(String inwords, String inword) {
String copyOfWords = inwords;
List<Integer> indicesOfWord = new ArrayList<Integer>();
int currentStartIndex = niwords.indexOf(inword);
int indexat = 0;
System.out.println(currentStartIndex);
while (cthing1 > 0) {
indicesOfWord.add(currentStartIndex+indexat);
System.out.println(currentStartIndex);
System.out.println(indicesOfWord);
indexat += cthing1;
copyOfWords = copyOfWords.substring(cthing1);
System.out.println(copyOfWords);
cthing1 = copyOfWords.indexOf(inword);
}
This problem can be solved in Python as follows:
这个问题可以在Python中解决如下:
indices = [m.start() for m in re.finditer(word, a.lower())]
where "word" is the word I'm looking for and "a" is the string I'm searching through.
其中“word”是我要查找的词,“a”是我要搜索的字符串。
How can I achieve this in Java?
我如何在 Java 中实现这一点?
采纳答案by Wiktor Stribi?ew
You can use capturing inside a positive look-ahead to get all overlapping matches and use Matcher#start
to get the indices of the captured substrings.
您可以在正向预测中使用捕获来获取所有重叠的匹配项并用于Matcher#start
获取捕获的子字符串的索引。
As for the regex, it will look like
至于正则表达式,它看起来像
(?=(aa))
In Java code:
在 Java 代码中:
String s = "aaaaaa";
Matcher m = Pattern.compile("(?=(aa))").matcher(s);
List<Integer> pos = new ArrayList<Integer>();
while (m.find())
{
pos.add(m.start());
}
System.out.println(pos);
Result:
结果:
[0, 1, 2, 3, 4]
See IDEONE demo
回答by Alex Hall
Using a regex is definitely an overly heavy solution for finding substrings, and it'll especially be a problem if your substring contains special regex characters like .
. Here's a solution adapted from this answer:
使用正则表达式对于查找子字符串绝对是一个过于繁重的解决方案,如果您的子字符串包含特殊的正则表达式字符,例如.
. 这是改编自此答案的解决方案:
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
List<Integer> result = new ArrayList<Integer>();
while(lastIndex != -1) {
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
result.add(lastIndex);
lastIndex += 1;
}
}