Latest web development tutorials

Java Examples - string search

Java Examples Java Examples

The following example uses the String class indexOf () method to find the position in the string substring appears, such as the presence of return over the position of occurrence of the string (the first bit is 0), if there is no return -1:

//SearchStringEmp.java 文件

public class SearchStringEmp{
   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);
      }
   }
}

The output is the above code examples:

Found Hello at index 0

Java Examples Java Examples