Java String Char Count countChars(String s, char c)

Here you can find the source of countChars(String s, char c)

Description

Returns the number of occurrences of a char in a string.

License

Open Source License

Parameter

Parameter Description
s String to be analyzed
c Char to be counted

Return

The number of occurrences of c in s

Declaration

public static int countChars(String s, char c) 

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*/
     * Returns the number of occurrences of a char in a string.
     * @param s String to be analyzed
     * @param c Char to be counted
     * @return The number of occurrences of <code>c</code> in <code>s</code>
     */
    public static int countChars(String s, char c) {
        int retVal = 0;
        for (int i = 0; i < s.length(); i++)
            retVal += s.charAt(i) == c ? 1 : 0;
        return retVal;
    }
}

Related

  1. countChars(String data)
  2. countChars(String input)
  3. countChars(String s, boolean countDigits, boolean countLetters, boolean countOthers)
  4. countChars(String s, char c)
  5. countChars(String s, char c)
  6. countChars(String s, String charset)
  7. countChars(String string, char c)
  8. countChars(String string, int character)
  9. countChars(String text, char ch, int from, int to)