Java程序搜索一组字符的最后一个索引
使用该lastIndexOf()方法搜索一组字符的最后一个索引。假设以下是我们的字符串。
String myStr = "pqrstuvwxyzpqrst";
在字符串中搜索子字符串“pqrs”以获取其出现在字符串中的最后一个索引。我们从索引3开始搜索。
int begnIndex = 3;
strLastIndex = myStr.indexOf("pqrs", begnIndex);以下是一个示例,其中我们正在搜索子字符串“pqrs”的最后一个索引
示例
public class Demo {
public static void main(String[] args) {
String myStr = "pqrstuvwxyzpqrst";
int strLastIndex = 0;
System.out.println("String: "+myStr);
strLastIndex = myStr.lastIndexOf("pqrs");
System.out.println("字符串中子字符串pqrs的最后一个索引 "+myStr+" = "+strLastIndex);
}
}输出结果
String: pqrstuvwxyzpqrst 字符串中子字符串pqrs的最后一个索引 pqrstuvwxyzpqrst = 11