In this post, we will see how to find and count occurrences of substring in string in java.
Table of Contents
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.
1 2 3 4 5 6 |
public int indexOf(String str) public int indexOf(String str, int fromIndex) public int indexOf(int char) public int indexOf(int char, int fromIndex) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; class FindOccurencesMain { public static void main(String[] args) { String myString = "This is my code, it is in Java."; String mySubstring= "is"; int count = 0, index = 0; List<Integer> indices=new ArrayList<>(); while ((index = myString.indexOf(mySubstring, index)) != -1 ){ count++; indices.add(index); index++; } System.out.println("Total occurrences of a substring in the given string: " + count); System.out.println("Indices of substring are: "+indices); } } |
Output:
Indices of substring are: [2, 5, 20]
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; class FindOccurencesMain { public static void main(String[] args) { String myString = "This is my code, it is in Java."; int count = 0, startIndex = 0; String mySubstring = "is"; List<Integer> indices=new ArrayList<>(); Pattern pattern = Pattern.compile(mySubstring); Matcher match = pattern.matcher(myString); while(match.find(startIndex)){ count++; indices.add(match.start()); startIndex = match.start() + 1; } System.out.println("Total occurrences of a substring in the given string: " + count); System.out.println("Indices of substring are: "+indices); } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; class FindOccurencesMain { public static void main(String[] args) { String myString = "This is my code, it is in Java."; int count = ( myString.split("is").length ) - 1; System.out.println("Total occurrences of a substring in the given string: " + count); } } |
Output:
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
.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; import org.apache.commons.lang3.StringUtils; class FindOccurencesMain { public static void main(String[] args) { String myString = "This is my code, it is in Java."; int count = StringUtils.countMatches(myString, "is"); System.out.println("Total occurrences of a substring in the given string: " + count); } } |
Output:
That’s all about how to find and count occurrences of substring in string in java.