Count SubString in a Large String in Java
Submitted by donbermoy on Thursday, January 21, 2016 - 15:22.
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:
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.
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.
To display the number of strings in the large string, have this code below:
Output:
Here's the full code of this tutorial:
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
- import java.io.*;
- return str == null || str.length() == 0;
- }
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.
- String string1,string2;
- string1 = in.readLine();
- string2 = in.readLine();
- import java.io.*;
- public class countSubStrings {
- String string1,string2;
- string1 = in.readLine();
- string2 = in.readLine();
- }
- return str == null || str.length() == 0;
- }
- if (isEmpty(str) || isEmpty(sub)) {
- return 0;
- }
- int count = 0;
- int idx = 0;
- while ((idx = str.indexOf(sub, idx)) != -1) {
- count++;
- idx += sub.length();
- }
- return count;
- }
- }
Add new comment
- 63 views