Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

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).

1 Answer

0 votes
by (46k points)

Here's one 'idiomatic one-liner' to count the occurrences of a char in a string:

int count = StringUtils.countMatches("a.b.c.d", ".");

Check out this common Lang doc.

or you can also try this Spring Framework's oneliner:

int occurrence= StringUtils.countOccurrencesOf("a.b.c.d", ".");

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...