How to count the number of occurrences of a char in a String?

Answer

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",".");

All string Questions

Ask your interview questions on string

Write Your comment or Questions if you want the answers on string from string Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---