Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 11, 2021

Java - Counting Substring Occurrences In A String

1. Overview

In this tutorial, We'll learn how to find the count of substring present in the input string in java.

Examples:

String: 222, substring = 22
Output: 2

String: madam, substring = ma
Output: 1

String: 1234, substring = 43
Output: 0
Java - Counting Substring Occurrences In A String


2. Java - Counting Substring Occurrences In A String using indexOf()


The below code is implemented based on the String class indexOf() method.
package com.javaprogramto.programs.strings.substring.count;

public class SubstringCountExample {

	public static void main(String[] args) {

		int count = countSubStringInString("222", "22");
		System.out.println(count);

		count = countSubStringInString("madam", "ma");
		System.out.println(count);

	}

	public static int countSubStringInString(String string, String toFind) {
		int position = 0;
		int count = 0;
		while ((position = string.indexOf(toFind, position)) != -1) {
			position = position + 1;
			count++;
		}
		return count;
	}

}

Output:
2
1

3. Java - Counting Substring Occurrences In A String using Pattern and Match

package com.javaprogramto.programs.strings.substring.count;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SubstringCountExample2 {

	public static void main(String[] args) {

		int count = countSubStringInString("222", "22");
		System.out.println(count);

		count = countSubStringInString("madam", "ma");
		System.out.println(count);

	}

	public static int countSubStringInString(String string, String toFind) {
		Pattern pattern = Pattern.compile(Pattern.quote(toFind));
		Matcher matcher = pattern.matcher(string);
		int position = 0;
		int count = 0;
		while (matcher.find(position)) {
			position = matcher.start() + 1;
			count++;
		}
		return count;
	}
}

This code also produces the same output as in above section.


4. Java - Counting Substring Occurrences In A String Using While Loop and charAt()

package com.javaprogramto.programs.strings.substring.count;

public class SubstringCountExample3 {

	public static void main(String[] args) {

		int count = countSubStringInString("22222", "22");
		System.out.println(count);

		count = countSubStringInString("madam", "ma");
		System.out.println(count);

	}

	public static int countSubStringInString(String string, String toFind) {
		int M = toFind.length();
		int N = string.length();
		int res = 0;

		for (int i = 0; i <= N - M; i++) {

			int j;
			for (j = 0; j < M; j++) {
				if (string.charAt(i + j) != toFind.charAt(j)) {
					break;
				}
			}

			if (j == M) {
				res++;
				j = 0;
			}
		}
		return res;
	}
}

Time complexity : O( M * N)

5. Conclusion


In this article, we've seen how to find the frequency of substring in the input string in java.



No comments:

Post a Comment

Please do not add any spam links in the comments section.