How to search a word inside a string?

Using the inbuilt "indexOf" method, the word inside a String can be found. In the given example I'm trying to find the "Hello" from the String.
public class SearchString{
   public static void main(String[] args) {
   
      String strOrig = "Hello readers";
      int intIndex = strOrig.indexOf("Hello");
      if(intIndex == - 1){
         System.out.println("Hello not found");
      } else {
         System.out.println("Found Hello at index "
         + intIndex);
      }
   }
}

Output:
Found Hello at index 0

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

    ReplyDelete