Home  >  Article  >  Java  >  Java Example - Find the last occurrence of a string

Java Example - Find the last occurrence of a string

黄舟
黄舟Original
2017-02-21 09:58:302292browse

In the following example, we use the string function strOrig.lastIndexOf(Stringname) to find the position where the substring Stringname appears in strOrig:

The example code is as follows:

//SearchlastString.java 文件public class SearchlastString {
   public static void main(String[] args) {
      String strOrig = "Hello world ,Hello Reader";
      int lastIndex = strOrig.lastIndexOf("Hello");
      if(lastIndex == - 1){
         System.out.println("Hello not found");
      }else{
         System.out.println("Last occurrence of Hello
         is at index "+ lastIndex);
      }
   }}

Above The output result of the code example is:

Last occurrence of Hello is at index 13

The above is the Java example - find the content of the last occurrence of a string, please pay attention to more related content 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