Why there is no countOf method in the String Class? [closed]

You’ll have to ask the JDK developers why they didn’t implement such a method, although it’s pretty easy to jimmy up such a utility yourself:

public static int countOf (String s, char c) {
    return s.length() - s.replace(c, "").length();
}

Or, if you’re willing to use third parties, there are a few standard implementations, such as Apache commons-lang StringUtils.countMatches.

Browse More Popular Posts

Leave a Comment