userimage

Need to count the number of occurrences of a char in a Java String

I have some text of strings where want to find the number of occurrences of a char.

e.g

A Message Element that has been added for technical reasons.

It has therefore no related Business Element and is message specific.

I want to find the occurrences of "e" or "b" or "i" etc. dynamically by passing the character from the above message.

userimage

Pass the character  as string to the below method which in return will give you the number of occurrences.


String message ="A Message Element that has been added for technical reasons.It has therefore no related Business Element and is message specific."

int getCount(String ch){

    int count = message.length() - message.replace(ch, "").length();

    return count;

}

Answer is