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

Code Factory
1 min readApr 24, 2020

--

Reference Link : Link

Donate : Link

CountMatches.java

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.";
}
}

Output :

"love" count : 3

Note : i used commons-lang3-3.1.jar for StringUtils.

--

--