Skip to main content

APIzation: Replication Package

[Q#5223815][A#5223857] How do I count the number of times a sequence occurs in a Java string?

https://stackoverflow.com/q/5223815

I have a String that looks like: I want to count the number of times is is in the string. How can I do this in Java?

Answer

https://stackoverflow.com/a/5223857

APIzation

int index = input.indexOf("is");
int count = 0;
while (index != -1) {
    count++;
    input = input.substring(index + 1);
    index = input.indexOf("is");
}
System.out.println("No of *is* in the input is : " + count);
package com.stackoverflow.api;

public class Human5223857 {

  public static int getCountOfWordInSentence(String input, String word) {
    int index = input.indexOf(word);
    int count = 0;
    while (index != -1) {
      count++;
      input = input.substring(index + 1);
      index = input.indexOf(word);
    }
    //System.out.println("No of *is* in the input is : " + count);
    return count;
  }
}

package com.stackoverflow.api;

/**
 * How do I count the number of times a sequence occurs in a Java string?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/5223857">https://stackoverflow.com/a/5223857</a>
 */
public class APIzator5223857 {

  public static int countNumber(String input) throws Exception {
    int index = input.indexOf("is");
    int count = 0;
    while (index != -1) {
      count++;
      input = input.substring(index + 1);
      index = input.indexOf("is");
    }
    return count;
  }
}