Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to show an example of searching Strings for subStrings

Posted By: Lynn Harris     Category: Java     Views: 5612

A Java Program to show an example of searching Strings for subStrings.

Code for Program to show an example of searching Strings for subStrings in Java

publicclass JAVA_051
 {


    publicstaticvoid main(String[] args)
    {
       String Text="To be or not to be, that is the question; Whether 'this"
                  +"nobler in the mind to suffer the sling and arrows of "
                  +"outrageous fortune, or to take arms against a sea of "
                  +"troubles, and by opposing end them?";

       System.out.println("Text paragraph is : \n" + Text + "\n");

       String Str="be";

       int index=Text.indexOf(Str);

       if(index!=-1)
       {
          System.out.println("From start, first \"" + Str +
                                 "\" is present at the position : " + index);

          index+=Str.length( );
       }

       else
          System.out.println("\"" + Str + "\" is not present in the above text.");

       index=Text.indexOf(Str,index);

       if(index!=-1)
          System.out.println("From start, second \"" + Str +
                                      "\" is present at the position : " + index);

       else
          System.out.println("\"" + Str + "\" is not present in the above text.");

       Str="of";

       index=Text.lastIndexOf(Str);

       if(index!=-1)
       {
          System.out.println("\nFrom end, first \"" + Str +
                                     "\" is present at the position : " + index);

          index-=Str.length( );
       }

       else
          System.out.println("\"" + Str + "\" is not present in the above text.");

       index=Text.lastIndexOf(Str,index);

       if(index!=-1)
          System.out.println("From end, second \"" + Str +
                                       "\" is present at the position : " + index);

       else
          System.out.println("\"" + Str + "\" is not present in the above text.");

    }
 }
  
Share: 



Lynn Harris
Lynn Harris author of Program to show an example of searching Strings for subStrings is from Columbus, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!