Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to show an example of searching Strings for characters

Posted By: Gary Long     Category: Java     Views: 2380

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

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

publicclass JAVA_050
 {
    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");

       int index=Text.indexOf('b');

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

       else
          System.out.println("'b' is not present in the above text.");

       index=Text.indexOf('b',++index);

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

       else
          System.out.println("'b' is not present in the above text.");

       index=Text.lastIndexOf('b');

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

       else
          System.out.println("'b' is not present in the above text.");

       index=Text.lastIndexOf('b',--index);

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

       else
          System.out.println("'b' is not present in the above text.");

    }
 }
  
Share: 



Gary Long
Gary Long author of Program to show an example of searching Strings for characters is from Los Angeles, 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!