java.lang.StringBuilder.lastIndexOf(String str)%uA0方法返回该字符串指定的子字符串的最右边出现处的索引。
声明
以下是java.lang.StringBuilder.lastIndexOf()方法的声明
public int lastIndexOf(String str)
参数
-
str%uA0-- 这是要搜索的字符串。
返回值
此方法返回最后一个子字符串的第一个字符的索引,如果字符串参数匹配一次或多次在这个对象中的子串。如果它不匹配一个子串,则返回-1。
异常
-
NullPointerException%uA0-- 如果%uA0str 为 null.
例子
下面的例子显示java.lang.StringBuilder.lastIndexOf()方法的使用。
package com.yiibai import java.lang.* public class StringBuilderDemo { public static void main(String[] args) { StringBuilder str = new StringBuilder("tutorials yiibai") System.out.println("string = " + str) /* returns the index within this string of the rightmost occurrence of the specified substring */ System.out.println("last Index of a = " + str.lastIndexOf("a")) // returns -1 as substring am is not found System.out.println("last index of am = " + str.lastIndexOf("am")) } }
让我们来编译和运行上面的程序,这将产生以下结果:
string = tutorials yiiai last Index of a = 6 last index of am = -1