简体   繁体   English

返回字符串“hi”出现在给定字符串中的任何位置的次数

[英]Return the number of times that the string “hi” appears anywhere in the given string

I wrote the following Java code, and it returned a time out error. 我编写了以下Java代码,它返回了一个超时错误。 I'm not exactly sure what that means nor why the code doesn't run 我不确定这意味着什么,也不知道为什么代码不会运行

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;
}

I know an alternative solution, using a for loop but I really thought this would work too. 我知道一个替代解决方案,使用for循环,但我真的认为这也会起作用。

You're getting into an infinite loop because pos never advances past the first match as the first match will be included in the substring. 你进入了一个无限循环,因为pos永远不会超过第一个匹配,因为第一个匹配将包含在子字符串中。

You can fix by using this overridden version of indexOf() inside your while loop: 您可以通过在while循环中使用此重写版本的indexOf()来修复:

pos = str.indexOf("hi", pos + 1);

Or use a do... while loop to avoid having to repeat the call to indexOf() : 或者使用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) Output the substring from the given index. str.substring(pos)输出给定索引的子字符串。 therefore in your code while loop never travel through your whole string and its stop at the first "hi".Use this. 因此在你的代码中,while循环永远不会遍历你的整个字符串,而是在第一个“hi”停止。使用它。

while(pos!=-1){
   count++;
   str = str.substring(pos+2);
   pos = str.indexOf("hi");
}

str variable store 2nd half of the string (use +2 for travel two more indexes for end of the hi) then check pos variable store that index of "hi" appear in the new string. str变量存储字符串的后半部分(使用+2表示另外两个索引用于hi的结尾)然后检查pos变量存储,“hi”索引出现在新字符串中。

Just for added fun...... 只是为了增加乐趣......

If the supplied substring (ie: "hi") is to be counted and it doesn't matter where it is located within the input string (single word or part of a word), you can use a one liner and let the String.replace() method do the job for you by actually removing the desired substring you want to count from the initial input string and calculating what remains of that input string (this does not modify the initial input string): 如果要计算提供的子字符串(即:“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);

Console will display: 3 控制台将显示: 3

You will note that the above code is letter case sensitive and therefore in the example above the substring "hi" differs from the word "Hi" because of the uppercase "H" so "Hi" is ignored. 您将注意到上面的代码是字母区分大小写的,因此在上面的示例中,子字符串“hi”与单词“Hi”不同,因为大写的“H”因此忽略“Hi” If you want to ignore letter case when counting for the supplied substrings then you can use the same code but utilize the String.toLowerCase() method within it: 如果要在计算所提供的子字符串时忽略字母大小写,则可以使用相同的代码,但在其中使用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);

Console will display: 4 控制台将显示: 4

If however the supplied substring you want to count is a specific word (not part of another word) then it gets a little more complicated. 但是,如果要计算的提供的子字符串是一个特定的单词(不是另一个单词的一部分 ),那么它会变得更复杂一些。 One way you can do this is by utilizing the Pattern and Matcher Classes along with a small Regular Expression . 一种方法是使用模式匹配器类以及一个小的正则表达式 It could look something like this: 它可能看起来像这样:

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);

Console will display: 1 控制台将显示: 1

Again, the above code is letter case sensitive. 同样,上面的代码是字母区分大小写的。 In other words if the supplied substring was "hi" then the display in console would of been 0 since "hi" is different from "Hi" which is in fact contained within the input string as the first word. 换句话说,如果提供的子字符串是“hi”,则控制台中的显示将为0,因为“hi”“Hi”不同, “Hi”实际上包含在输入字符串中作为第一个字。 If you want to ignore letter case then it would just be a matter of converting both the input string and the supplied substring to either all upper case or all lowercase, for example: 如果你想忽略字母大小写,那么只需将输入字符串和提供的子字符串转换为全大写或全小写,例如:

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);

Console will display: 1 控制台将显示: 1

As you can see in the two most recent code examples above the Regular Expression (RegEx) of "\\\\bHi\\\\b" was used (in code, a variable was used in the place of Hi ) and here is what it means: 正如您在上面的两个最新代码示例中所看到的那样,使用了"\\\\bHi\\\\b"的正则表达式(RegEx)(在代码中,变量用于代替Hi ),这就是它的含义:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM