java.lang.String.lastIndexOf(int ch, int fromIndex)%uA0方法返回此字符串指定字符的最后出现处的索引,向后搜索从指定的索引处。
声明
以下是java.lang.String.lastIndexOf()方法的声明
public int lastIndexOf(int ch, int fromIndex)
参数
-
ch%uA0-- 这是字符的值(Unicode代码点)。
-
fromIndex%uA0-- 这是索引搜索的开始。如果它大于或等于该字符串的长度,它具有相同的效果,好像它是等于一个小于该字符串的长度:这整个字符串可能被搜索。如果是负的,它具有相同的效果,如果它是-1:返回-1。
返回值
此方法返回字符对小于或等于fromIndex表示的字符序列的最后一个匹配项的索引,或-1,如果该字符不会在该点之前出现。
异常
-
NA
例子
下面的例子显示java.lang.String.lastIndexOf()方法的使用。
package com.yiibai import java.lang.* public class StringDemo { public static void main(String[] args) { String str = "This is yiibai" /* returns positive value(last occurrence of character t) as character is located, which searches character t backward till index 14 */ System.out.println("last index of letter &apost&apos = " + str.lastIndexOf(&apost&apos, 14)) /* returns -1 as character is not located under the give index, which searches character s backward till index 2 */ System.out.println("last index of letter &aposs&apos = " + str.lastIndexOf(&aposs&apos, 2)) // returns -1 as character e is not in the string System.out.println("last index of letter &apose&apos = " + str.lastIndexOf(&apose&apos, 5)) } }
让我们来编译和运行上面的程序,这将产生以下结果:
last index of letter &apost&apos = 10 last index of letter &aposs&apos = -1 last index of letter &apose&apos = -1