Java Char Count countCharacters(String str, char chr)

Here you can find the source of countCharacters(String str, char chr)

Description

Counts the number of chr's in str.

License

Open Source License

Parameter

Parameter Description
str The string to check for instances of <code>chr</code>.
chr The character to check for.

Return

The number of times chr occurs in str.

Declaration

public static int countCharacters(String str, char chr) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* ww w . ja  v a  2 s . co  m*/
     * Counts the number of <code>chr</code>'s in <code>str</code>.
     * 
     * @param str
     *            The string to check for instances of <code>chr</code>.
     * @param chr
     *            The character to check for.
     * @return The number of times <code>chr</code> occurs in <code>str</code>.
     */
    public static int countCharacters(String str, char chr) {
        int ret = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == chr) {
                ret++;
            }
        }
        return ret;
    }
}

Related

  1. countChar(String a_text, char a_character)
  2. countCharacter(final String sourceString, final char lookFor)
  3. countCharacter(String content, char character)
  4. countCharacterOccurrences(String string, char c)
  5. countCharacters(String code, char c, int start, int end)
  6. countCharInString(String s, char c)
  7. countChars( final String testString, final char match )
  8. countChars(final String str, final char c)
  9. countChars(final String str, final char chr)