Copyright © tutorialspoint.com
How to search the last position of a substring ?
This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.
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); } } } |
The above code sample will produce the following result.
Last occurrence of Hello is at index 13 |
Copyright © tutorialspoint.com