Java String Match Count countMatches(String str, String match)

Here you can find the source of countMatches(String str, String match)

Description

Counts the occurence of a String in another String

License

Open Source License

Parameter

Parameter Description
str The String to be searched
match The String to be searched for

Return

How often the searched string has been found

Declaration

public static int countMatches(String str, String match) 

Method Source Code

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

public class Main {
    /**//from   w  w  w  .  ja  v  a 2  s  . c o m
     * Counts the occurence of a String in another String
     * 
     * @param str
     *            The String to be searched
     * @param match
     *            The String to be searched for
     * @return How often the searched string has been found
     */
    public static int countMatches(String str, String match) {
        int counter = 0;

        while (str.contains(match)) {
            counter++;

            str = str.substring(str.indexOf(match) + match.length());
        }

        return counter;
    }
}

Related

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