Home  >  Article  >  Java  >  Java Example - String Search

Java Example - String Search

黄舟
黄舟Original
2017-02-21 10:11:111727browse

The following example uses the indexOf() method of the String class to find the position where the substring appears in the string. If it exists, it returns the position where the string appears (the first bit is 0). If it does not exist, it returns -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 result of the above code example is:

Found Hello at index 0

The above is the content of Java example-string search. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn