substring()
String 类提供了两个重载的 substring() 方法。

substring(int beginIndex)
这个方法用于从指定的索引位置开始,一直截取到字符串的末尾。
语法
public String substring(int beginIndex)
参数
beginIndex(int): 开始截取的索引位置(包含此位置的字符)。
返回值
一个新的 String 对象,包含从 beginIndex 开始到原字符串末尾的所有字符。
重要概念:索引
Java 字符串的索引从 0 开始。
字符串 "Hello":
'H'的索引是0'e'的索引是1'l'的索引是2'l'的索引是3'o'的索引是4
示例
public class SubstringExample {
public static void main(String[] args) {
String str = "Hello, World!";
// 从索引 7 开始,截取到末尾
String sub1 = str.substring(7);
System.out.println("原始字符串: " + str);
System.out.println("从索引 7 开始的子串: " + sub1); // 输出: World!
// 从索引 0 开始,截取到末尾 (相当于复制整个字符串)
String sub2 = str.substring(0);
System.out.println("从索引 0 开始的子串: " + sub2); // 输出: Hello, World!
// 获取文件名
String filePath = "/home/user/document.txt";
String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
System.out.println("文件名: " + fileName); // 输出: document.txt
}
}
异常
beginIndex 的值是负数,或者大于字符串的长度 (str.length()),会抛出 StringIndexOutOfBoundsException。

String str = "Hello"; // String sub = str.substring(5); // 空字符串 "" // String sub = str.substring(6); // 抛出 StringIndexOutOfBoundsException
substring(int beginIndex, int endIndex)
这个方法用于从指定的开始索引截取到指定的结束索引(不包含结束索引处的字符)。
语法
public String substring(int beginIndex, int endIndex)
参数
beginIndex(int): 开始截取的索引位置(包含此位置的字符)。endIndex(int): 结束截取的索引位置(不包含此位置的字符)。
返回值
一个新的 String 对象,包含从 beginIndex 到 endIndex - 1 的所有字符。
示例
public class SubstringExample2 {
public static void main(String[] args) {
String str = "Hello, World!";
// 从索引 7 开始,到索引 12 结束 (不包含 12)
String sub1 = str.substring(7, 12);
System.out.println("原始字符串: " + str);
System.out.println("从索引 7 到 12 的子串: " + sub1); // 输出: World
// 获取第一个单词
String firstWord = str.substring(0, 5);
System.out.println("第一个单词: " + firstWord); // 输出: Hello
// 获取第二个单词 (包括逗号和空格)
String secondWordWithPunct = str.substring(5, 13);
System.out.println("第二个单词(含标点): " + secondWordWithPunct); // 输出: , Worl
}
}
异常
如果以下任一条件为真,会抛出 StringIndexOutOfBoundsException:
beginIndex为负数。endIndex为负数。beginIndex大于endIndex。beginIndex或endIndex大于字符串的长度 (str.length())。
String str = "Hello"; // String sub = str.substring(2, 2); // 空字符串 "" // String sub = str.substring(3, 1); // 抛出 StringIndexOutOfBoundsException // String sub = str.substring(1, 5); // 抛出 StringIndexOutOfBoundsException
其他相关操作
除了 substring(),还有一些其他方法可以用来“获取”字符串中的特定部分。

获取单个字符:charAt(int index)
如果你想获取字符串中某一个特定位置的字符,而不是一个子串,应该使用 charAt()。
String str = "Hello";
char firstChar = str.charAt(0); // 获取 'H'
char lastChar = str.charAt(str.length() - 1); // 获取 'o'
System.out.println("第一个字符: " + firstChar); // 输出: H
System.out.println("最后一个字符: " + lastChar); // 输出: o
分割字符串:split(String regex)
如果你想根据某个分隔符(如逗号、空格)把一个字符串分割成多个子字符串,应该使用 split() 方法,它返回一个字符串数组 (String[])。
String csvData = "apple,banana,orange,grape";
String[] fruits = csvData.split(",");
System.out.println(fruits[0]); // 输出: apple
System.out.println(fruits[1]); // 输出: banana
System.out.println(fruits[2]); // 输出: orange
System.out.println(fruits[3]); // 输出: grape
String sentence = "This is a simple sentence";
String[] words = sentence.split(" ");
for (String word : words) {
System.out.println(word);
}
提取前后部分:startsWith() 和 endsWith()
这些方法用于检查字符串是否以特定子串开头或结尾,并返回一个布尔值 (boolean),虽然它们不直接“返回”子串,但在判断字符串格式时非常有用。
String path = "/home/user/file.txt";
boolean isAbsolute = path.startsWith("/");
boolean isTextFile = path.endsWith(".txt");
System.out.println("是绝对路径吗? " + isAbsolute); // 输出: true
System.out.println("是文本文件吗? " + isTextFile); // 输出: true
总结与最佳实践
| 目标 | 推荐方法 | 示例 | 返回类型 |
|---|---|---|---|
| 从某个位置截取到末尾 | substring(beginIndex) |
str.substring(7) |
String |
| 截取一个范围内的子串 | substring(beginIndex, endIndex) |
str.substring(0, 5) |
String |
| 获取单个字符 | charAt(index) |
str.charAt(0) |
char |
| 按分隔符分割字符串 | split(regex) |
str.split(",") |
String[] |
| 检查是否以某子串开头 | startsWith(prefix) |
str.startsWith("http") |
boolean |
| 检查是否以某子串结尾 | endsWith(suffix) |
str.endsWith(".java") |
boolean |
重要提示:不可变性
String 对象在 Java 中是不可变的,这意味着 substring() 方法不会修改原始字符串,而是返回一个新的字符串对象,原始字符串 str 的内容保持不变。
String original = "Hello";
String modified = original.substring(1, 3);
System.out.println("原始字符串: " + original); // 输出仍然是: Hello
System.out.println("新字符串: " + modified); // 输出: el 