Java - How to Count the Number of Occurrences of Substring in a String
In this section, we will write a Java program to Count the Number of Occurrences of Substring in a String. Java program to Count the Number of Occurrences of Substring in a String In the below program, we have countOccurrencesOf(String str, String sub) a generic method, here we simply pass input string and substring as arguments and method return number of occurrences of the substring. /** * * Java program to count number of occurrence of substring in given string. * @author knowledgefactory.net * */ public class Main { public static void main ( String [] args) { int count = countOccurrencesOf ( "javaknowledgefactoryjava" , "java" ); System . out .println( "Count number of occurrences of substring 'java' " + " in string 'javaknowledgefactoryjava' : " + count ); int count1 = countOccurrencesOf ( "javaknowledgefactoryjavajava" , "java" ); System .