查找一组字符的最后一个索引的 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("The last index of substring pqrs in the string "+myStr+" = "+strLastIndex); } }
输出
String: pqrstuvwxyzpqrst The last index of substring pqrs in the string pqrstuvwxyzpqrst = 11
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言教程
C++
C#
MongoDB
MySQL
Javascript
PHP