Java String Match Count countMatches(String string, char toMatch)

Here you can find the source of countMatches(String string, char toMatch)

Description

Count the number of times a specific character is present in a string

License

Open Source License

Parameter

Parameter Description
string the string to test
toMatch the character to look for

Return

the number of times toMatch is present in string

Declaration

protected static int countMatches(String string, char toMatch) 

Method Source Code

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

public class Main {
    /**//from   w  ww .  j  a  v  a2  s  . c  om
     * Count the number of times a specific character is present in a string
     *
     * @param string
     *            the string to test
     * @param toMatch
     *            the character to look for
     * @return the number of times toMatch is present in string
     */
    protected static int countMatches(String string, char toMatch) {
        int occurrences = 0;
        for (char c : string.toCharArray()) {
            if (c == toMatch) {
                occurrences++;
            }
        }
        return occurrences;
    }
}

Related

  1. countMatches(String str, String sub)
  2. countMatches(String str, String sub)
  3. countMatches(String str, String sub)
  4. countMatches(String str, String sub)
  5. countMatches(String string, char find)
  6. countMatches(String string, String other)
  7. countMatches(String theString, String... sep)
  8. countMatches(String[] query, String[] entry)
  9. countMatchingLines(Throwable ex, Throwable cause)