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

No comments:

Post a Comment