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. String string1,string2;
  2.  
  3. System.out.print("Enter large string:");
  4. string1 = in.readLine();
  5. System.out.print("Enter substring:");
  6. 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. String string1,string2;
  5.  
  6. System.out.print("Enter large string:");
  7. string1 = in.readLine();
  8. System.out.print("Enter substring:");
  9. string2 = in.readLine();
  10.  
  11. System.out.print("Number of substrings found: " + countMatches(string1,string2));
  12. }
  13. public static boolean isEmpty(String str) {
  14. return str == null || str.length() == 0;
  15. }
  16. public static int countMatches(String str, String sub) {
  17. if (isEmpty(str) || isEmpty(sub)) {
  18. return 0;
  19. }
  20. int count = 0;
  21. int idx = 0;
  22. while ((idx = str.indexOf(sub, idx)) != -1) {
  23. count++;
  24. idx += sub.length();
  25. }
  26. return count;
  27. }
  28.  
  29.  
  30. }
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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
13 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.