Possible Duplicate:
How do I count the number of occurrences of a char in a String?
I am looking to count the occurrences of a specific character, for example "i" that occurs in the input.
Example, from standard input:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
This returns 6
, because there are 6 occurrences of the letter "i" (case matters).
This could be accomplished with a loop stepping through the string with charAt(i)
.
Is there a more elegant way to accomplish this?