• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Count SubString in a Large String in Java

Hyreal

Virtual Warrior
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
137
Likes
146
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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. 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:

countsubstring.png


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. System

    .out

    .print

    (

    "Enter large string:"

    )

    ;
  7. string1 =

    in.readLine

    (

    )

    ;
  8. System

    .out

    .print

    (

    "Enter substring:"

    )

    ;
  9. string2 =

    in.readLine

    (

    )

    ;

  10. System

    .out

    .print

    (

    "Number of substrings found: "

    +

    countMatches(

    string1,string2)

    )

    ;
  11. }
  12. public

    static

    boolean

    isEmpty(

    String

    str)

    {
  13. return

    str ==

    null

    ||

    str.length

    (

    )

    ==

    0

    ;
  14. }
  15. public

    static

    int

    countMatches(

    String

    str, String

    sub)

    {
  16. if

    (

    isEmpty(

    str)

    ||

    isEmpty(

    sub)

    )

    {
  17. return

    0

    ;
  18. }
  19. int

    count =

    0

    ;
  20. int

    idx =

    0

    ;
  21. while

    (

    (

    idx =

    str.indexOf

    (

    sub, idx)

    )

    !=

    -

    1

    )

    {
  22. count++;
  23. idx +=

    sub.length

    (

    )

    ;
  24. }
  25. return

    count;
  26. }


  27. }

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

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,501

352,162

352,176

Top