String.prototype.substring()
String
值的 substring()
方法返回该字符串从起始索引到结束索引(不包括结束索引)的部分,或者如果未提供结束索引则返回到字符串的末尾。
¥The substring()
method of String
values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
Try it
语法
参数
返回值
描述
¥Description
substring()
提取从 indexStart
到 indexEnd
的字符(但不包括 indexEnd
)。尤其:
¥substring()
extracts characters from indexStart
up to but not including indexEnd
. In particular:
- 如果省略
indexEnd
,则substring()
将字符提取到字符串末尾。 - 如果
indexStart
等于indexEnd
,则substring()
返回空字符串。 - 如果
indexStart
大于indexEnd
,则substring()
的效果就好像两个参数交换了;请参见下面的示例。
任何小于 0
或大于 str.length
的参数值将分别被视为 0
和 str.length
。
¥Any argument value that is less than 0
or greater than str.length
is treated as if it were 0
and str.length
, respectively.
任何 NaN
的参数值都被视为 0
。
¥Any argument value that is NaN
is treated as if it were 0
.
示例
使用子字符串()
¥Using substring()
以下示例使用 substring()
显示字符串 "Mozilla"
中的字符:
¥The following example uses substring()
to display characters from the
string "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"
使用带有 length 属性的 substring()
¥Using substring() with length property
以下示例使用 substring()
方法和 length
属性来提取特定字符串的最后一个字符。鉴于你不需要像上面的示例中那样知道开始和结束索引,因此这种方法可能更容易记住。
¥The following example uses the substring()
method and
length
property to extract the last characters of a
particular string. This method may be easier to remember, given that you don't need to
know the starting and ending indices as you would in the above examples.
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() 之间的区别
¥The difference between substring() and substr()
substring()
和 substr()
方法之间存在细微差别,因此你应该小心不要将它们混淆。
¥There are subtle differences between the substring()
and
substr()
methods, so you should be careful not to get
them confused.
substr()
的两个参数是start
和length
,而substring()
的两个参数是start
和end
。- 如果
substr()
的start
索引为负数,则它会换行到字符串的末尾,而substring()
会将其钳位到0
。 substr()
中的负长度被视为零,而如果end
小于start
,则substring()
将交换两个索引。
此外,substr()
被认为是 ECMAScript 中的遗留功能,因此最好尽可能避免使用它。
¥Furthermore, substr()
is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
substring() 和 slice() 之间的区别
¥Differences between substring() and slice()
substring()
和 slice()
方法几乎相同,但两者之间存在一些细微的差异,特别是在处理否定参数的方式上。
¥The substring()
and slice()
methods are
almost identical, but there are a couple of subtle differences between the two,
especially in the way negative arguments are dealt with.
如果 indexStart
大于 indexEnd
,则 substring()
方法会交换其两个参数,这意味着仍返回字符串。如果是这种情况,slice()
方法将返回空字符串。
¥The substring()
method swaps its two arguments if
indexStart
is greater than indexEnd
,
meaning that a string is still returned. The slice()
method returns an empty string if this is the case.
const text = "Mozilla";
console.log(text.substring(5, 2)); // "zil"
console.log(text.slice(5, 2)); // ""
如果其中一个或两个参数为负数或 NaN
,则 substring()
方法将它们视为 0
。
¥If either or both of the arguments are negative or NaN
, the
substring()
method treats them as if they were 0
.
console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
slice()
还将 NaN
参数视为 0
,但当给它负值时,它会从字符串末尾向后计数以查找索引。
¥slice()
also treats NaN
arguments as 0
, but when
it is given negative values it counts backwards from the end of the string to find the
indexes.
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
有关负数的更多示例,请参阅 slice()
页。
¥See the slice()
page for more examples with negative
numbers.
替换字符串中的子字符串
¥Replacing a substring within a string
以下示例替换字符串中的子字符串。它将替换单个字符和子字符串。示例末尾的函数调用从原始字符串 Brave New World
创建字符串 Brave New Web
。
¥The following example replaces a substring within a string. It will replace both individual characters and substrings. The function call at the end of the example creates a string Brave New Web
from the original string Brave New World
.
// 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"
。
¥Note that this can result in an infinite loop if oldS
is itself a
substring of newS
— for example, if you attempted to replace
"World"
with "OtherWorld"
here.
更好的替换字符串的方法如下:
¥A better method for replacing strings is as follows:
function replaceString(oldS, newS, fullS) {
return fullS.split(oldS).join(newS);
}
上面的代码作为子字符串操作的示例。如果你需要替换子字符串,大多数时候你会想要使用 String.prototype.replace()
。
¥The code above serves as an example for substring operations. If you need to replace
substrings, most of the time you will want to use
String.prototype.replace()
.
规范
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.substring |
浏览器兼容性
BCD tables only load in the browser