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.

Was this post helpful?

Related Posts

  • 28 October

    Convert UUID to String in Java

    Table of ContentsIntroductionUUID class in JavaConvert UUID to String in Java Introduction In this article, we will have a look on How to Convert UUID to String in Java. We will also shed some light on the concept of UUID, its use, and its corresponding representation in Java class. Let us have a quick look […]

  • 27 September

    Repeat String N times in Java

    Table of ContentsIntroductionWhat is a String in Java?Repeat String N times in JavaSimple For Loop to Repeat String N Times in JavaUsing Recursion to Repeat String N Times in JavaString.format() method to Repeat String N Times in JavaUsing String.repeat() method in Java 11 to Repeat String N TimesRegular Expression – Regex to Repeat String N […]

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-63da582a26ad2785044490/] [crayon-63da582a26ad5946934882/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

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.