Count SubString in a Large String in Java

This tutorial will teach you how to create a program that will have an example of Caret Event and Listener in Java. A CaretEvent lets the user notify interested parties that the text caret has changed in the event with its source. Note: A caret is the cursor indicating the insertion point So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of countSubStrings.java. 2. Import the io package library:
  1. import java.io.*;
3. Now, create a method named isEmpty as Boolean that has a String str as parameter. This will trigger to return 0 if the string doesn't have a substring inputted by the user.
  1.            public static boolean isEmpty(String str) {
  2.       return str == null || str.length() == 0;
  3.   }
4. Create again another method named countMatches that has the parameters of String str, String sub that will trigger to count how may substring are there in the inputted string.
  1.       public static int countMatches(String str, String sub) {
  2.       if (isEmpty(str) || isEmpty(sub)) {
  3.           return 0;
  4.       }
  5.       int count = 0;
  6.       int idx = 0;
  7.       while ((idx = str.indexOf(sub, idx)) != -1) {
  8.           count++;
  9.           idx += sub.length();
  10.       }
  11.       return count;
  12.   }
5. Now in your Main, create two String variables named string1,string2, and in variable for the inputting. Then get the input from the user.
  1.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  2.         String string1,string2;
  3.        
  4.         System.out.print("Enter large string:");
  5.         string1 = in.readLine();
  6.         System.out.print("Enter substring:");
  7.         string2 = in.readLine();
To display the number of strings in the large string, have this code below:
  1.         System.out.print("Number of substrings found:  " + countMatches(string1,string2));
Output:
output Here's the full code of this tutorial:
  1. import java.io.*;
  2. public class countSubStrings {
  3.         public static void main(String args[]) throws IOException{
  4.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  5.         String string1,string2;
  6.        
  7.         System.out.print("Enter large string:");
  8.         string1 = in.readLine();
  9.         System.out.print("Enter substring:");
  10.         string2 = in.readLine();
  11.        
  12.         System.out.print("Number of substrings found:  " + countMatches(string1,string2));
  13.          }
  14.            public static boolean isEmpty(String str) {
  15.       return str == null || str.length() == 0;
  16.   }
  17.       public static int countMatches(String str, String sub) {
  18.       if (isEmpty(str) || isEmpty(sub)) {
  19.           return 0;
  20.       }
  21.       int count = 0;
  22.       int idx = 0;
  23.       while ((idx = str.indexOf(sub, idx)) != -1) {
  24.           count++;
  25.           idx += sub.length();
  26.       }
  27.       return count;
  28.   }
  29.  
  30.  
  31. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment