Java的String中的subString()方法

本文详细解析了Java中String类的substring方法,介绍了如何通过指定的开始和结束索引来截取字符串。通过实例展示了如何获取特定长度的子串,特别强调了结束索引是不包含在内的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@TOC

方法如下:

public String substring(int beginIndex, int endIndex)
第一个int为开始的索引,对应String数字中的开始位置,
第二个是截止的索引位置,对应String中的结束位置
1、取得的字符串长度为:endIndex - beginIndex;
2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符
如:
“hamburger”.substring(4, 8) returns “urge”
“smiles”.substring(1, 5) returns “mile”
取长度大于等于3的字符串a的后三个子字符串,只需a.subString(a.length()-3, a.length());

手册中的具体说明如下:
substring
public String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:

“hamburger”.substring(4, 8) returns “urge”
“smiles”.substring(1, 5) returns “mile”

Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

<think>我们正在讨论Java String类的substring方法。根据引用,substring方法用于从字符串中提取子串,有两个重载版本。下面我将详细解释如何使用这两个方法。</think>### Java String substring() 方法详解 Java 的 `substring()` 方法String 类中最常用的方法之一,用于从原始字符串中提取指定位置的子字符串。该方法有两个重载版本,均返回新字符串(原始字符串保持不变,因为 StringJava 中是不可变的)[^2][^3]。 #### 1. **基本语法** ```java // 提取从 beginIndex 开始到字符串末尾的子串 String substring(int beginIndex) // 提取从 beginIndex 开始到 endIndex 前一位的子串 String substring(int beginIndex, int endIndex) ``` #### 2. **参数说明** | 参数 | 说明 | 取值范围 | |------|------|----------| | `beginIndex` | 子串起始索引(包含) | $[0, \text{str.length()})$ | | `endIndex` | 子串结束索引(不包含) | $[\text{beginIndex}, \text{str.length()}]$ | > **索引规则**:Java 字符串索引从 0 开始,最大索引为 $\text{length()} - 1$。 #### 3. **使用示例** 假设原始字符串:`String str = "Hello, World!";`(长度 = 13) | 方法调用 | 返回值 | 说明 | |----------|--------|------| | `str.substring(7)` | `"World!"` | 从索引 7 开始到末尾 | | `str.substring(0, 5)` | `"Hello"` | 索引 0-4(不含 5) | | `str.substring(7, 12)` | `"World"` | 索引 7-11(不含 12) | | `str.substring(13)` | `""`(空字符串) | 索引等于长度时返回空串 | | `str.substring(2, 2)` | `""` | 起始索引 = 结束索引时返回空串 | #### 4. **边界情况处理** - **索引越界**:会抛出 `StringIndexOutOfBoundsException` ```java str.substring(-1); // 错误:索引 < 0 str.substring(14); // 错误:索引 > length() str.substring(5, 20); // 错误:endIndex > length() ``` - **特殊截取场景**: ```java // 获取最后5个字符 str.substring(str.length() - 5); // "orld!" // 获取第一个字符 str.substring(0, 1); // "H" ``` #### 5. **实际应用场景** 1. **提取身份证出生日期**(假设格式:`"110105199001011234"`) ```java String id = "110105199001011234"; String birth = id.substring(6, 14); // "19900101" ``` 2. **文件路径截取** ```java String path = "/home/user/docs/report.txt"; String fileName = path.substring(path.lastIndexOf("/") + 1); // "report.txt" ``` 3. **关键词提取** ```java String log = "ERROR: Database connection failed"; if (log.startsWith("ERROR")) { String msg = log.substring(7); // "Database connection failed" } ``` #### 6. **重要注意事项** 1. **索引包含规则**:子串包含 `beginIndex` 字符,不包含 `endIndex` 字符 - 数学表示:子串 = $\text{str}[ \text{beginIndex}, \text{endIndex} )$ 2. **内存优化**(Java 7+): - Java 7 前:子串共享原始字符数组,可能导致内存泄漏 - Java 7+:子串创建新数组,牺牲性能换安全性[^1] 3. **与 `split()` 对比**: - `substring()`:按位置截取 - `split()`:按分隔符拆分(返回数组) > 完整示例代码: > ```java > public class SubstringDemo { > public static void main(String[] args) { > String text = "Java Programming"; > System.out.println(text.substring(5)); // "Programming" > System.out.println(text.substring(0, 4)); // "Java" > System.out.println(text.substring(5, 9)); // "Prog" > } > } > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值