Java.lang.String.lastIndexOf() 方法
声明
以下是 java.lang.String.lastIndexOf() 方法的声明。
public int lastIndexOf(int ch)
参数
ch − 这是字符的值(Unicode 代码点)。
返回值
此方法返回此对象表示的字符序列中字符最后一次出现的索引,如果字符未出现,则返回 -1。
异常
NA
示例
下面的例子展示了 java.lang.String.lastIndexOf() 方法的使用。
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'));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
last index of letter 'i' = 19 last index of letter 'e' = -1

