Easy Tutorial
❮ Java9 Stream Api Improvements Java String Replace ❯

Java Example - Finding the Last Occurrence of a String

Java Examples

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

Example code is as follows:

SearchlastString.java File

public class SearchlastString {
   public static void main(String[] args) {
      String strOrig = "Hello world ,Hello tutorialpro";
      int lastIndex = strOrig.lastIndexOf("tutorialpro");
      if(lastIndex == - 1){
         System.out.println("String tutorialpro not found");
      }else{
         System.out.println("Last occurrence of tutorialpro string: " + lastIndex);
      }
   }
}

The output of the above code example is:

Last occurrence of tutorialpro string: 19

Java Examples

❮ Java9 Stream Api Improvements Java String Replace ❯