Java String Occurence Count countOccurences(String text, String target)

Here you can find the source of countOccurences(String text, String target)

Description

Counts the number of occurrences of a String within another String.

License

BSD License

Parameter

Parameter Description
text the String to search
target the String to search for

Return

the number of occurrences of target within text

Declaration

public static int countOccurences(String text, String target) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

public class Main {
    /**//ww  w  .j  a  v  a  2  s  .  c o  m
     * Counts the number of occurrences of a String within another String.
     * 
     * @param text the String to search
     * @param target the String to search for
     * @return the number of occurrences of target within text
     */
    public static int countOccurences(String text, String target) {
        return text == null || target == null || !text.contains(target) ? 0
                : 1 + countOccurences(text.substring(text.indexOf(target) + 1), target);
    }
}

Related

  1. countOccurences(String s, char c)
  2. countOccurences(String str, int ch)
  3. countOccurences(String str, String subStr)
  4. countOccurences(String text, char find)
  5. countOccurences(String text, String subtext)
  6. countOccurences(String textToSearch, String pattern)
  7. countOccurencesOfChar(String str, char charToCount)