I have the string
a.b.c.d
I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner.
(Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop).
Using Eclipse Collections
If you have more than one character to count, you can use a
CharBag
as follows:Note: I am a committer for Eclipse Collections.
The simplest way to get the answer is as follow:
Why not just split on the character and then get the length of the resulting array. array length will always be number of instances + 1. Right?
My 'idiomatic one-liner' for this is:
Why write it yourself when it's already in commons lang?
Spring Framework's oneliner for this is: