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


Donate : Link

Medium Blog : Link

Applications : 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s