Java String Lastindexof
# Java lastIndexOf() Method
[Java String Class](#)
* * *
The lastIndexOf() method has the following four forms:
* **public int lastIndexOf(int ch):** Returns the index of the last occurrence of the specified character in this string, or -1 if the string does not contain such a character.
* **public int lastIndexOf(int ch, int fromIndex):** Returns the index of the last occurrence of the specified character in this string, searching backwards starting from the specified index, or -1 if the string does not contain such a character.
* **public int lastIndexOf(String str):** Returns the index of the rightmost occurrence of the specified substring in this string, or -1 if the string does not contain such a substring.
* **public int lastIndexOf(String str, int fromIndex):** Returns the index of the last occurrence of the specified substring in this string, searching backwards starting from the specified index, or -1 if the string does not contain such a substring.
### Syntax
public int lastIndexOf(int ch) or public int lastIndexOf(int ch, int fromIndex) or public int lastIndexOf(String str) or public int lastIndexOf(String str, int fromIndex)
### Parameters
* **ch** -- a character.
* **fromIndex** -- the index to start searching from.
* **str** -- the substring to search for.
### Return Value
The index value of the first occurrence of the specified substring in the string.
### Example
public class Test {public static void main(String args[]) {String Str = new String("Tutorial:www.");String SubStr1 = new String("tutorial");String SubStr2 = new String("com");System.out.print("Last occurrence of character 'o' :" );System.out.println(Str.lastIndexOf( 'o' ));System.out.print("Last occurrence of character 'o' from index 14 :" );System.out.println(Str.lastIndexOf( 'o', 14 ));System.out.print("Last occurrence of substring SubStr1 :" );System.out.println( Str.lastIndexOf( SubStr1 ));System.out.print("Last occurrence of substring SubStr1 from index 15 :" );System.out.println( Str.lastIndexOf( SubStr1, 15 ));System.out.print("Last occurrence of substring SubStr2 :" );System.out.println(Str.lastIndexOf( SubStr2 ));}}
The output of the above program is:
Last occurrence of character 'o' :17Last occurrence of character 'o' from index 14 :13Last occurrence of substring SubStr1 :9Last occurrence of substring SubStr1 from index 15 :9Last occurrence of substring SubStr2 :16
* * Java String Class](#)
YouTip