Java.lang.String.lastIndexOf() Method
Advertisements
Description
The java.lang.String.lastIndexOf(int ch) method returns the index within this string of the last occurrence of the specified character.
Declaration
Following is the declaration for java.lang.String.lastIndexOf() method
public int lastIndexOf(int ch)
Parameters
ch -- This is the value of character(Unicode code point).
Return Value
This method returns the index of the last occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
Exception
NA
Example
The following example shows the usage of java.lang.String.lastIndexOf() method.
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "This is tutorialspoint";
// returns the index of last occurrence of character i
System.out.println("last index of letter 'i' = "
+ str.lastIndexOf('i'));
// returns -1 as character e is not in the string
System.out.println("last index of letter 'e' = "
+ str.lastIndexOf('e'));
}
}
Let us compile and run the above program, this will produce the following result:
last index of letter 'i' = 19 last index of letter 'e' = -1