Java String Match Count countMatches(String s, String sb)

Here you can find the source of countMatches(String s, String sb)

Description

Retrieve how many times is the substring in the larger string.

License

Open Source License

Parameter

Parameter Description
s the string to check
sb the substring to count

Return

the number of occurances, 0 if the string is null

Declaration

public static int countMatches(String s, String sb) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*ww w. j ava  2 s . co m*/
     * Retrieve how many times is the substring in the larger string. Null
     * returns 0.
     * 
     * @param s
     *            the string to check
     * @param sb
     *            the substring to count
     * @return the number of occurances, 0 if the string is null
     */
    public static int countMatches(String s, String sb) {
        if (s == null || sb == null) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = s.indexOf(sb, idx)) != -1) {
            count++;
            idx += sb.length();
        }
        return count;
    }
}

Related

  1. countMatches(final CharSequence str, final CharSequence sub)
  2. countMatches(final CharSequence str, final CharSequence sub)
  3. countMatches(final String str, final String sub)
  4. countMatches(String line, char chr)
  5. countMatches(String reference, String query)
  6. countMatches(String str, String match)
  7. countMatches(String str, String sub)
  8. countMatches(String str, String sub)
  9. countMatches(String str, String sub)