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 |
瀏覽器相容性
載入中…