Java 字符串处理:String 与 StringBuilder 详解
1. String 类方法介绍
1.1 lastIndexOf 方法
lastIndexOf 方法用于定位字符串中某个字符最后一次出现的位置,它从字符串的末尾开始向前搜索。若找到该字符,则返回其在字符串中的索引;若未找到,则返回 -1。该方法有两种形式:
- 一种接受字符的整数表示作为参数。
- 另一种接受两个整数参数,分别为字符的整数表示和开始反向搜索的索引。
以下是示例代码:
public class LastIndexOfExample {
public static void main(String[] args) {
String str = "abcabc";
int index1 = str.lastIndexOf('a');
int index2 = str.lastIndexOf('a', 3);
System.out.println("Last index of 'a': " + index1);
System.out.println("Last index of 'a' starting from index 3: " + index2);
}
}
1.2 indexOf 和 lastIndexOf 搜索子字符串
这两个方法的另一种重载形式接受一个字符串作为第一个参数,用于搜索指定的字符序列(子字符串
超级会员免费看
订阅专栏 解锁全文
1323

被折叠的 条评论
为什么被折叠?



