Counting occurrences of a substring or character in a string


countMatches method from org.apache.commons.lang3.StringUtils is typically used to count occurrences of a substring or character in a String:

import org.apache.commons.lang3.StringUtils;
 
String newPhrase = "Some words here, some words there, some more words everywhere";
String stringTarget = "words";
int stringOccurrences = StringUtils.countMatches(newPhrase, stringTarget); // 3
 
char newCharTarget = 'e';
int charOccurrences = StringUtils.countMatches(newPhrase, newCharTarget); // 6
 

Otherwise for does the same with standard Java API's you could use Regular Expressions:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
String newText = "New string, more strings, and even more strings!";
System.out.println(countOccurrences("string", newText)); // prints 3
System.out.println(countOccurrences("more", newText)); // prints 2
 
public static int countOccurrences(String searchTerm, String newText)
{
    Pattern pattern = Pattern.compile(searchTerm);
    Matcher matcher = pattern.matcher(newText);
 
    int occurrences = 0;
    while (matcher.find())
	{
        occurrences++;
    }
    return occurrences;
}

This code snippet is a Java program that counts the occurrences of a specified search term within a given text using regular expressions (java.util.regex). Here's an explanation:

Import Statements:

  • import java.util.regex.Matcher; and import java.util.regex.Pattern;: These import statements bring in the necessary classes from the java.util.regex package to work with regular expressions.

Variables:

  • String newText = "New string, more strings, and even more strings!";: This string contains the text in which we'll search for occurrences of specific substrings.

Method Invocation:

  • countOccurrences("string", newText) and countOccurrences("more", newText): These method invocations count the occurrences of the substrings "string" and "more" in the newText string.

Method countOccurrences():

  • public static int countOccurrences(String searchTerm, String newText) { ... }: This method takes two parameters: searchTerm (the substring to search for) and newText (the text in which to search).
  • Pattern pattern = Pattern.compile(searchTerm);: This line compiles the search term into a Pattern object, enabling pattern matching with the specified term.
  • Matcher matcher = pattern.matcher(newText);: It creates a Matcher object by applying the compiled pattern to the newText.
  • int occurrences = 0;: This variable will hold the count of occurrences found.
  • while (matcher.find()) { occurrences++; }: This loop iterates through the text and uses the find() method of the Matcher class to find each occurrence of the search term. For each match found, it increments the occurrences counter.
  • return occurrences;: Finally, the method returns the total count of occurrences found.

The method essentially uses the Pattern and Matcher classes to find and count occurrences of the specified search term within the given text using regular expressions.

Basic Programs