# java.lang.StringIndexOutOfBoundsException: 字符串索引超出范围: 10

- **ID:** `java/string-index-out-of-bounds-exception`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

尝试使用负数或大于等于字符串长度的索引访问字符串中的字符，通常由差一错误或格式错误的输入引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   Check the string length before accessing: if (index >= 0 && index < str.length()) { char c = str.charAt(index); } else { /* handle error */ }
   ```
2. ```
   Use str.substring(start, end) with bounds checking, or use str.charAt(Math.min(index, str.length()-1)) as a temporary clamp.
   ```

## 无效尝试

- **** — This hides the bug and may cause downstream issues like null pointer exceptions or incorrect data processing. (70% 失败率)
- **** — This does not address the root cause of incorrect index calculation; the index may still be out of range for other inputs. (85% 失败率)
