snippetjavaCritical
How do I count the number of occurrences of a char in a String?
Viewed 0 times
howtheoccurrencesnumbercharstringcount
Problem
I have the string
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).
a.b.c.dI 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).
Solution
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:
int count = StringUtils.countMatches("a.b.c.d", ".");Why write it yourself when it's already in commons lang?
Spring Framework's oneliner for this is:
int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");Code Snippets
int count = StringUtils.countMatches("a.b.c.d", ".");int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");Context
Stack Overflow Q#275944, score: 789
Revisions (0)
No revisions yet.