![](/img/trans.png)
[英]Return the number of times that the string “hello” appears anywhere in the given string
[英]Return the number of times that the string “hi” appears anywhere in the given string
我编写了以下Java代码,它返回了一个超时错误。 我不确定这意味着什么,也不知道为什么代码不会运行
public int countHi(String str) {
int pos = str.indexOf("hi");
int count = 0;
while(pos!=-1)
{
count++;
pos = str.substring(pos).indexOf("hi");
}
return count;
}
我知道一个替代解决方案,使用for循环,但我真的认为这也会起作用。
你进入了一个无限循环,因为pos
永远不会超过第一个匹配,因为第一个匹配将包含在子字符串中。
您可以通过在while
循环中使用此重写版本的indexOf()
来修复:
pos = str.indexOf("hi", pos + 1);
或者使用do... while
循环以避免重复调用indexOf()
:
public static int countHi(String str) {
int pos = -1, count = -1;
do {
count++;
pos = str.indexOf("hi", pos + 1);
} while (pos != -1);
return count;
}
str.substring(pos)
输出给定索引的子字符串。 因此在你的代码中,while循环永远不会遍历你的整个字符串,而是在第一个“hi”停止。使用它。
while(pos!=-1){
count++;
str = str.substring(pos+2);
pos = str.indexOf("hi");
}
str变量存储字符串的后半部分(使用+2表示另外两个索引用于hi的结尾)然后检查pos变量存储,“hi”索引出现在新字符串中。
只是为了增加乐趣......
如果要计算提供的子字符串(即:“hi”)并且它在输入字符串(单个单词或单词的一部分)中的位置无关紧要,则可以使用单个字符串并使字符串。 replace()方法通过从初始输入字符串中实际删除想要计数的所需子字符串并计算该输入字符串的剩余部分(这不会修改初始输入字符串)来为您完成工作:
String inputString = "Hi there. This is a hit in his pocket";
String subString = "hi";
int count = (inputString.length() - inputString.replace(subString, "").
length()) / subString.length())
//Display the result...
System.out.println(count);
控制台将显示: 3
您将注意到上面的代码是字母区分大小写的,因此在上面的示例中,子字符串“hi”与单词“Hi”不同,因为大写的“H”因此忽略“Hi” 。 如果要在计算所提供的子字符串时忽略字母大小写,则可以使用相同的代码,但在其中使用String.toLowerCase()方法:
String inputString = "Hi there. This is a hit in his pocket";
String subString = "hi";
int count = (inputString.length() - inputString.toLowerCase().
replace(substring.toLowerCase(), "").
length()) / substring.length())
//Display the result...
System.out.println(count);
控制台将显示: 4
但是,如果要计算的提供的子字符串是一个特定的单词(不是另一个单词的一部分 ),那么它会变得更复杂一些。 一种方法是使用模式和匹配器类以及一个小的正则表达式 。 它可能看起来像这样:
String inputString = "Hi there. This is a hit in his pocket";
String subString = "Hi";
String regEx = "\\b" + subString + "\\b";
int count = 0; // To hold the word count
// Compile the regular expression
Pattern p = Pattern.compile(regEx);
// See if there are matches of subString within the
// input string utilizing the compiled pattern
Matcher m = p.matcher(inputString);
// Count the matches found
while (m.find()) {
count++;
}
//Display the count result...
System.out.println(count);
控制台将显示: 1
同样,上面的代码是字母区分大小写的。 换句话说,如果提供的子字符串是“hi”,则控制台中的显示将为0,因为“hi”与“Hi”不同, “Hi”实际上包含在输入字符串中作为第一个字。 如果你想忽略字母大小写,那么只需将输入字符串和提供的子字符串转换为全大写或全小写,例如:
String inputString = "Hi there. This is a hit in his pocket";
String subString = "this is";
String regEx = "\\b" + subString.toLowerCase() + "\\b";
int count = 0; // To hold the word count
// Compile the regular expression
Pattern p = Pattern.compile(regEx);
// See if there are matches of subString within the
// input string utilizing the compiled pattern
Matcher m = p.matcher(inputString.toLowerCase());
// Count the matches found
while (m.find()) {
count++;
}
//Display the count result...
System.out.println(count);
控制台将显示: 1
正如您在上面的两个最新代码示例中所看到的那样,使用了"\\\\bHi\\\\b"
的正则表达式(RegEx)(在代码中,变量用于代替Hi ),这就是它的含义:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.