String.prototype.substring()
String 值的 substring() 方法返回此字符串中从开始索引到但不包含结束索引的部分,如果未提供结束索引,则返回到字符串末尾的部分。
试一试
const str = "Mozilla";
console.log(str.substring(1, 3));
// Expected output: "oz"
console.log(str.substring(2));
// Expected output: "zilla"
语法
substring(indexStart)
substring(indexStart, indexEnd)
参数
indexStart-
要包含在返回的子字符串中的第一个字符的索引。
indexEnd可选-
要从返回的子字符串中排除的第一个字符的索引。
返回值
一个包含给定字符串指定部分的新字符串。
描述
substring() 从 indexStart 提取字符,直到但不包括 indexEnd。具体来说:
- 如果
indexEnd被省略或为undefined,substring()会提取到字符串末尾的字符。 - 如果
indexStart等于indexEnd,substring()将返回一个空字符串。 - 如果
indexStart大于indexEnd,则substring()的效果就好像两个参数被交换了;请参阅下面的示例。
任何小于 0 或大于 str.length 的参数值将被分别视为 0 和 str.length。
任何 NaN 参数值将被视为 0。
示例
使用 substring()
以下示例使用 substring() 显示字符串 "Mozilla" 的字符。
const anyString = "Mozilla";
console.log(anyString.substring(0, 1)); // "M"
console.log(anyString.substring(1, 0)); // "M"
console.log(anyString.substring(0, 6)); // "Mozill"
console.log(anyString.substring(4)); // "lla"
console.log(anyString.substring(4, 7)); // "lla"
console.log(anyString.substring(7, 4)); // "lla"
console.log(anyString.substring(0, 7)); // "Mozilla"
console.log(anyString.substring(0, 10)); // "Mozilla"
将 substring() 与 length 属性结合使用
以下示例使用 substring() 方法和 length 属性来提取特定字符串的最后几个字符。此方法可能更容易记住,因为您不需要像上面示例那样知道起始和结束索引。
const text = "Mozilla";
// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"
// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
substring() 和 substr() 之间的区别
substring() 和 substr() 方法之间存在细微差别,因此您应该小心不要混淆它们。
substr()的两个参数是start和length,而substring()的参数是start和end。- 如果
substr()的start索引为负数,它将回绕到字符串的末尾,而substring()会将其限制为0。 substr()中的负长度被视为零,而substring()如果end小于start,则会交换两个索引。
此外,substr() 被认为是ECMAScript 中的遗留功能,因此最好避免使用它。
const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
substring() 和 slice() 之间的区别
substring() 和 slice() 方法几乎相同,但在处理负参数方面存在一些细微差别。
如果 indexStart 大于 indexEnd,substring() 方法会交换其两个参数,这意味着仍然会返回一个字符串。如果出现这种情况,slice() 方法会返回一个空字符串。
const text = "Mozilla";
console.log(text.substring(5, 2)); // "zil"
console.log(text.slice(5, 2)); // ""
如果任一或所有参数为负数或 NaN,substring() 方法会将它们视为 0。
console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
slice() 也将 NaN 参数视为 0,但当它接收到负值时,它会从字符串末尾开始倒数以查找索引。
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
有关负数的更多示例,请参阅 slice() 页面。
替换字符串中的子字符串
以下示例替换字符串中的子字符串。它将替换单个字符和子字符串。示例末尾的函数调用从原始字符串 "Brave New World" 创建了字符串 "Brave New Web"。
// Replaces oldS with newS in the string fullS
function replaceString(oldS, newS, fullS) {
for (let i = 0; i < fullS.length; ++i) {
if (fullS.substring(i, i + oldS.length) === oldS) {
fullS =
fullS.substring(0, i) +
newS +
fullS.substring(i + oldS.length, fullS.length);
}
}
return fullS;
}
replaceString("World", "Web", "Brave New World");
请注意,如果 oldS 本身是 newS 的子字符串,这可能会导致无限循环——例如,如果您尝试在此处将 "World" 替换为 "OtherWorld"。
更好的替换字符串的方法如下
function replaceString(oldS, newS, fullS) {
return fullS.split(oldS).join(newS);
}
上面的代码是子字符串操作的示例。如果您需要替换子字符串,大多数情况下您会想使用 String.prototype.replace()。
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-string.prototype.substring |
浏览器兼容性
加载中…