Java String Handling RegEx

Count occurrences of substring in string in Java example

Count occurrences of substring in string example shows how to count occurrences of substring in string in Java. Example also shows how to count number of occurrences of substring using various ways.

How to count occurrences of substring in string in Java?

There are several ways using which you can count occurrences of substring in Java.

1) Count occurrences of substring using indexOf method

You can count occurrences of substring in string using indexOf method of String class. Consider below given string.

Below given is the example program to find number of occurrences of “Java” within the string.

Output

We started off with having count and fromIndex as 0. fromIndex holds the index position from where we want to search the substring. In while loop, we find substring, assign the index of next occurrence to fromIndex and check if the returned value is greater than -1. indexOf method returns -1 if substring is not found in the string, otherwise it returns the index of the substring.

Inside the while loop, we increment the count of occurrence of substring. We also increment the fromIndex by 1. That is because when we find the substring, next time we want to search the substring after that index. If we don’t do that, we will end up with infinite loop.

2) Count occurrences of substring using split method

You can use split method as given below to count the substrings.

Output

split method returns an array of matching string parts. So basically, we are splitting a string with the substring we want to find and checking how many array elements it has returned.

Important Note: split method accepts the regular expression. While using above approach, beware of the regular expression metacharacters (characters which have special meaning in regular expression). Consider below given example.

Output

Surprising result? We do not have “C++” in our string yet our program says it has found it 1 time. That is because “+” sign has a special meaning in regular expression and it means “one or more”. We do have “C” one or more times so searching “C++” returns 1. In order to avoid such errors, always use quote method of Pattern class whenever you want to do a literal search using split method as given below.

Output

3) Count occurrences of substring using Apache Commons library

If you are using Apache Commons library, you can use countMatches method of StringUtils class to count occurrences of substring in string as given below.

Output

4) Count occurrences of substring using regular expression

You can also use regular expression Pattern class to find substring and count it’s occurrences as given below.

Output

This approach is similar to the indexOf approach except that it uses find method of Matcher class to find a match. Patter.LITERAL flag will ignore regular expression metacharacters and treat them as literal.

Please let us know your views in the comments section below.

 

Join 1000+ fellow learners! Enter your email address below: