Easy Tutorial
❮ Java Hashmap Remove Java Package ❯

Java Example - String Search

Java Examples

The following example uses the indexOf() method of the String class to find the position of a substring within a string. It returns the position of the substring if it exists (with the first position being 0), or -1 if it does not exist:

SearchStringEmp.java File

public class SearchStringEmp {
   public static void main(String[] args) {
      String strOrig = "Google tutorialpro Taobao";
      int intIndex = strOrig.indexOf("tutorialpro");
      if(intIndex == - 1){
         System.out.println("Substring tutorialpro not found");
      }else{
         System.out.println("Substring tutorialpro found at position " + intIndex);
      }
   }
}

The output of the above code example is:

Substring tutorialpro found at position 7

Java Examples

❮ Java Hashmap Remove Java Package ❯