为什么以下算法不会停止?(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);
Run Code Online (Sandbox Code Playgroud)
A_M*_*A_M 179
如何使用Apache Commons Lang的StringUtils.countMatches?
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(StringUtils.countMatches(str, findStr));
Run Code Online (Sandbox Code Playgroud)
那输出:
3
Run Code Online (Sandbox Code Playgroud)
Oli*_*ier 114
你lastIndex += findStr.length();被放在括号外面,造成一个无限循环(当没有发现时,lastIndex总是如此findStr.length()).
这是固定版本:
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);
Run Code Online (Sandbox Code Playgroud)
Pet*_*rey 86
更短的版本.;)
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(str.split(findStr, -1).length-1);
Run Code Online (Sandbox Code Playgroud)
cod*_*ach 82
最后一行是造成问题.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);
Run Code Online (Sandbox Code Playgroud)
Jea*_*ean 79
你真的必须自己处理匹配吗?特别是如果你需要的只是出现次数,正则表达式更整洁:
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);
Run Code Online (Sandbox Code Playgroud)
在这里,它包含在一个漂亮且可重用的方法中:
public static int count(String text, String find) {
int index = 0, count = 0, length = find.length();
while( (index = text.indexOf(find, index)) != -1 ) {
index += length; count++;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
循环结束时计数为3; 希望能帮助到你
我很惊讶没有人提到这一支班轮。它简单,简洁,并且比str.split(target, -1).length-1
public static int count(String str, String target) {
return (str.length() - str.replace(target, "").length()) / target.length();
}
Run Code Online (Sandbox Code Playgroud)
许多给定的答案在以下一个或多个方面失败:
这是我写的:
static int countMatches(Pattern pattern, String string)
{
Matcher matcher = pattern.matcher(string);
int count = 0;
int pos = 0;
while (matcher.find(pos))
{
count++;
pos = matcher.start() + 1;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
示例电话:
Pattern pattern = Pattern.compile("232");
int count = countMatches(pattern, "23232"); // Returns 2
Run Code Online (Sandbox Code Playgroud)
如果你想要一个非正则表达式搜索,只需使用LITERAL标志适当地编译你的模式:
Pattern pattern = Pattern.compile("1+1", Pattern.LITERAL);
int count = countMatches(pattern, "1+1+1"); // Returns 2
Run Code Online (Sandbox Code Playgroud)
您可以使用内置库函数的出现次数:
import org.springframework.util.StringUtils;
StringUtils.countOccurrencesOf(result, "R-")
Run Code Online (Sandbox Code Playgroud)
public int countOfOccurrences(String str, String subStr) {
return (str.length() - str.replaceAll(Pattern.quote(subStr), "").length()) / subStr.length();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300532 次 |
| 最近记录: |