Count the occurrences of the substring in string - Android java.lang

Android examples for java.lang:String Search

Description

Count the occurrences of the substring in string

Demo Code

public class Main {

  /**//w  ww .ja v  a 2s .  c om
   * Count the occurrences of the substring in string s.
   *
   * @param str
   *          string to search in. Return 0 if this is null.
   * @param sub
   *          string to search for. Return 0 if this is null.
   */
  public static int countOccurrencesOf(String str, String sub) {
    if (str == null || sub == null || str.length() == 0 || sub.length() == 0) {
      return 0;
    }
    int count = 0;
    int pos = 0;
    int idx;
    while ((idx = str.indexOf(sub, pos)) != -1) {
      ++count;
      pos = idx + sub.length();
    }
    return count;
  }

}

Related Tutorials