Count The Occurrences of a String In Another String | Code Factory

package com.codeFactory;import org.apache.commons.lang3.StringUtils;/**
* @author code.factory
*
*/
public class CountMatches {
public static void main(String... args) {
System.out.println("\"love\" count : " + StringUtils.countMatches(StringUtils.lowerCase(getText()), "love"));
}

private static String getText() {
return "Love is a friendship set to music.\n"
+ "The more one judges, the less one love.\n"
+ "Life without love is like a tree without blossoms or fruit.";
}
}
"love" count : 3

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Who is looking at your code (quality)?

Continuous Delivery for Kubernetes Applications

Asset bundles in Unity

Exception File Handling

Java — How to read a File | Code Factory

How I Went From Being a Technophobic Law Grad to a Web Developer

A dynamic CPQ for Dynamics users

The Fallacy of Individual Contributor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Java as a first programming language?

Deadly Diamond of Death

What’s new for developers in Java 18

How Java Works