Find occurrences of a substring in a string in Java
This post will discuss how to find the total number of occurrences of one string in another string in Java. A null or a string input should return 0.
1. Using indexOf()
method
The idea is to use the indexOf()
method of the String
class, which returns the index within this string of the first occurrence of the specified substring, starting at the specified index. It returns -1
if there is no such occurrence.
The trick is to start from the position where the last found substring ends. This is demonstrated below:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
class Main { /* Checks if a string is empty ("") or null. */ public static boolean isEmpty(String s) { return s == null || s.length() == 0; } /* Counts how many times the substring appears in the larger string. */ public static int countMatches(String text, String str) { if (isEmpty(text) || isEmpty(str)) { return 0; } int index = 0, count = 0; while (true) { index = text.indexOf(str, index); if (index != -1) { count ++; index += str.length(); } else { break; } } return count; } public static void main(String[] args) { String text = "AABCCAAADCBBAADBBC"; String str = "AA"; int count = countMatches(text, str); System.out.println(count); } } |
Output:
3
2. Using split()
method
The tricky solution is to use the split()
method to split the string around the given substring. This is demonstrated below:
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 |
class Main { /* Checks if a string is empty ("") or null. */ public static boolean isEmpty(String s) { return s == null || s.length() == 0; } /* Counts how many times the substring appears in the larger string. */ public static int countMatches(String text, String str) { if (isEmpty(text) || isEmpty(str)) { return 0; } return text.split(str, -1).length - 1; } public static void main(String[] args) { String text = "AABCCAAADCBBAADBBC"; String str = "AA"; int count = countMatches(text, str); System.out.println(count); } } |
Output:
3
3. Using Pattern matching
We can also take the help of regular expressions in Java to count the substring frequency, as shown below:
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 27 28 29 30 31 32 33 34 35 36 |
import java.util.regex.Matcher; import java.util.regex.Pattern; class Main { /* Checks if a string is empty ("") or null. */ public static boolean isEmpty(String s) { return s == null || s.length() == 0; } /* Counts how many times the substring appears in the larger string. */ public static int countMatches(String text, String str) { if (isEmpty(text) || isEmpty(str)) { return 0; } Matcher matcher = Pattern.compile(str).matcher(text); int count = 0; while (matcher.find()) { count++; } return count; } public static void main(String[] args) { String text = "AABCCAAADCBBAADBBC"; String str = "AA"; int count = countMatches(text, str); System.out.println(count); } } |
Output:
3
4. Using Apache Commons Lang
Finally, we can leverage the Apache Commons Lang library, which has the countMatches()
method included in the StringUtils
class for this exact purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String text = "AABCCAAADCBBAADBBC"; String str = "AA"; int count = StringUtils.countMatches(text, str); System.out.println(count); } } |
Output:
3
That’s all about finding the occurrences of a substring in a string in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)