Java String search sub string from end

Description

Java String search sub string from end


public class Main {
  public static void main(String[] args) {
    String letters = "abcdefghijklmabcdefghijklm";

    // test lastIndexOf to find a substring in a string
    System.out.printf("Last \"def\" is located at index %d\n",
       letters.lastIndexOf("def"));
    /*from   w w  w .  j  a  v  a 2  s .  com*/
    int fromPosition = 25;
    
    System.out.printf("Last \"def\" is located at index %d\n",
       letters.lastIndexOf("def", fromPosition));
    System.out.printf("Last \"hello\" is located at index %d\n",
       letters.lastIndexOf("hello"));

  }
}



PreviousNext

Related