Find and count occurrences of substring in string in java

Find and count occurences of substring in String in Java

In this post, we will see how to find and count occurrences of substring in string in java.

Using the indexOf() method

The indexOf() method in java is a specialized function to find the index of the first occurrence of a substring in a string. This method has 4 overloads.

We will use the second overload as we have to check the entire string. The fromIndex parameter is used to specify the starting index from where to start the search. This method returns an integer value indicating the position of the occurrence. If it returns -1, then it means that there exists no occurrence in the given string.

Output:

Total occurrences of a substring in the given string: 3
Indices of substring are: [2, 5, 20]

Using regular expression

The regular expression uses a specific syntax or pattern for matching text. We can use a regular expression to match the pattern of our substring and find its occurrences in the given string. In Java, we use the Pattern and Matcher class of regular expression to find the pattern and then match it. It is one of the simplest ways to find the number of occurrences of a substring in a string.

Output:

Total occurrences of a substring in the given string: 3
Indices of substring are: [2, 5, 20]

We have found the occurrences of the substring in the given string using the find() method of the Matcher class. After finding the occurrences, we have counted them by incrementing the count variable.

Using split() method

The split() method in java is used to split a string based on some substring. We will simply use this method with a little logic to find the number of occurrences of a substring in a string. We know that if there exists one pattern of a substring in the given string, then the method will divide the given string into two parts. Using this logic, we will find the length of the array of strings produced as a result of the split() method and subtract 1 from it to find the number of occurrences.

Output:

Total occurrences of a substring in the given string: 3

Using the countMatches() method from Apache Commons library

The StringUtils class of Apache Commons library contains the countMatches() method that can be used to find all occurrences of a substring in a string.
Here is the dependency which you need to add for Apache common lang3 in pom.xml.

Output:

Total occurrences of a substring in the given string: 3

That’s all about how to find and count occurrences of substring in string in java.

Related Posts

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

  • Java String contains IgnoreCase
    23 June

    Java String contains IgnoreCase

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusion We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains IgnoreCase checks. […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote character 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-60ef2fa6e31ae230610596/] When you print on console using System.out.println, you use double quotes and […]

  • Java Split String by Space
    13 May

    Java split string by space

    Learn about how to split String in java in different ways.

  • Split String by new line in java
    09 May

    How to split String by newline in java

    Learn about how to split String by newline in java

  • 15 April

    How to declare a String array in java

    Learn about how to declare String array in java.

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.