Java - Write code to Count the number of times the given char is in the string

Requirements

Write code to Count the number of times the given char is in the string

Demo

import static org.apache.camel.util.StringQuoteHelper.doubleQuote;

public class Main{
    public static void main(String[] argv){
        String s = "book2s.com";
        char ch = 'a';
        System.out.println(countChar(s,ch));
    }/* w w  w.  ja  va 2 s  . c o m*/
    /**
     * Counts the number of times the given char is in the string
     *
     * @param s  the string
     * @param ch the char
     * @return number of times char is located in the string
     */
    public static int countChar(String s, char ch) {
        if (ObjectHelper.isEmpty(s)) {
            return 0;
        }

        int matches = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (ch == c) {
                matches++;
            }
        }

        return matches;
    }
}