How do I count the number of occurrences of a char in a String

0 votes

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

Dec 30, 2020 in Java by Rajiv
• 8,870 points
2,033 views

1 answer to this question.

0 votes

My 'idiomatic one-liner' 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", ".");
answered Dec 30, 2020 by Gitika
• 65,770 points

Related Questions In Java

0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
5,662 views
0 votes
0 answers

How do I replace all occurrences of a string in JavaScript?

Given a string: s = "Test abc test ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
569 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,074 views
0 votes
2 answers

How do we convert only the second letter of every word in a string to uppercase ?

The following code will perform your desired ...READ MORE

answered Aug 29, 2018 in Java by curious
• 560 points
11,995 views
0 votes
1 answer

Get sub-string from text based on certain conditions/rules

Hello @laiman, You can use IsMatch function that tests whether ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,840 points
1,136 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,701 views
0 votes
2 answers

Generate an alpha-numeric string randomly

Java supplies a way of doing this ...READ MORE

answered Jul 18, 2018 in Java by Daisy
• 8,140 points
2,618 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,620 points
1,186 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,480 points
209,927 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
1,252 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP