Java 字符串中子字符串的出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/767759/
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
Occurrences of substring in a string
提问by
Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)
为什么以下算法对我没有停止?(str 是我要搜索的字符串,findStr 是我要查找的字符串)
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr,lastIndex);
if( lastIndex != -1)
count++;
lastIndex += findStr.length();
}
System.out.println(count);
采纳答案by codebreach
The last line was creating a problem. lastIndex
would never be at -1, so there would be an infinite loop. This can be fixed by moving the last line of code into the if block.
最后一行是创建一个问题。lastIndex
永远不会在-1,所以会有一个无限循环。这可以通过将最后一行代码移动到 if 块中来解决。
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
count ++;
lastIndex += findStr.length();
}
}
System.out.println(count);
回答by Stanislav Kniazev
Increment lastIndex
whenever you look for next occurrence.
lastIndex
每当您查找下一次出现时递增。
Otherwise it's always finding the first substring (at position 0).
否则它总是找到第一个子字符串(在位置 0)。
回答by Bhushan Bhangale
public int indexOf(int ch,
int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
返回此字符串中第一次出现指定字符的索引,从指定索引开始搜索。
So your lastindex
value is always 0 and it always finds helloin the string.
所以你的lastindex
值总是 0 并且它总是在字符串中找到hello。
回答by Thorsten Schleinzer
try adding lastIndex+=findStr.length()
to the end of your loop, otherwise you will end up in an endless loop because once you found the substring, you are trying to find it again and again from the same last position.
尝试添加lastIndex+=findStr.length()
到循环的末尾,否则您将陷入无限循环,因为一旦找到子字符串,您就会试图从同一个最后一个位置一次又一次地找到它。
回答by dfa
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while((lastIndex = str.indexOf(findStr, lastIndex)) != -1) {
count++;
lastIndex += findStr.length() - 1;
}
System.out.println(count);
at the end of the loop count is 3; hope it helps
循环结束时计数为 3;希望能帮助到你
回答by Jean
Do you really have to handle the matching yourself ? Especially if all you need is the number of occurences, regular expressions are tidier :
你真的必须自己处理匹配吗?特别是如果您只需要出现次数,则正则表达式会更整洁:
String str = "helloslkhellodjladfjhello";
Pattern p = Pattern.compile("hello");
Matcher m = p.matcher(str);
int count = 0;
while (m.find()){
count +=1;
}
System.out.println(count);
回答by Olivier
Your lastIndex += findStr.length();
was placed outside the brackets, causing an infinite loop (when no occurence was found, lastIndex was always to findStr.length()
).
您lastIndex += findStr.length();
被放置在括号外,导致无限循环(当没有发现出现时, lastIndex 总是 to findStr.length()
)。
Here is the fixed version :
这是固定版本:
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += findStr.length();
}
}
System.out.println(count);
回答by Peter Lawrey
A shorter version. ;)
较短的版本。;)
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(str.split(findStr, -1).length-1);
回答by A_M
How about using StringUtils.countMatchesfrom Apache Commons Lang?
使用Apache Commons Lang 的StringUtils.countMatches怎么样?
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(StringUtils.countMatches(str, findStr));
That outputs:
输出:
3
回答by Xander
Try this one. It replaces all the matches with a -
.
试试这个。它将所有匹配项替换为-
.
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int numberOfMatches = 0;
while (str.contains(findStr)){
str = str.replaceFirst(findStr, "-");
numberOfMatches++;
}
And if you don't want to destroy your str
you can create a new string with the same content:
如果你不想破坏你的str
你可以创建一个具有相同内容的新字符串:
String str = "helloslkhellodjladfjhello";
String strDestroy = str;
String findStr = "hello";
int numberOfMatches = 0;
while (strDestroy.contains(findStr)){
strDestroy = strDestroy.replaceFirst(findStr, "-");
numberOfMatches++;
}
After executing this block these will be your values:
执行此块后,这些将是您的值:
str = "helloslkhellodjladfjhello"
strDestroy = "-slk-djladfj-"
findStr = "hello"
numberOfMatches = 3