How to count the occurrences of a Sub String in another String?

How to count the occurrences of a Sub String in the given String > String search

Problem Statements:

  1. I have a long String, I want to find out the occurrence of sub string in this.
  2. How to find out a duplicate sub string in a long String.
  3. I want to print multiple occurrence count of sub String.
 Source Code:
 package com;  
 import org.springframework.util.StringUtils;

public class Test {
	 * @param args
	public static void main(String args[]) {

		String str = "Java Technology J2EE Technology JSP Technology Servlet Technology Spring Technology Hibernate Technology Jboss Technology"; // count

		System.out.println("Count By Using Spring Framework:: \n"
				+ StringUtils.countOccurrencesOf(str, "Technology"));

		System.out.println("Count By Using Apache String Util Class:: \n"
				+ org.apache.commons.lang.StringUtils.countMatches(str,

		System.out.println("Count By Using While Loop:: \n"
				+ CharCount(str, "Technology"));


	public static int CharCount(String str, String searchStr) {
		int occurence = 0;
		int count = 0;

		while ((count = str.indexOf(searchStr, count)) != -1) {

		return occurence;



Count By Using Spring Framework 
Count By Using Apache String Util Class 
Count By Using While Loop 
7 Tutorials Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *