Home > Java > javaTutorial > body text

Java Example - String Search

黄舟
Release: 2017-02-21 10:11:11
Original
1742 people have browsed it

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);
      }
   }}
Copy after login

The output result of the above code example is:

Found Hello at index 0
Copy after login

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)!


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template