substring()

從來源字串擷取從某個索引開始到字元串結尾的子字串。

(選擇性) 您可以指定所要求子字串的長度。

語法

substring(source,startingIndex [,length])

深入瞭解 語法慣例

參數

名稱 類型 必要 Description
source string ✔️ 要從中取得子字串的字串。
startingIndex int ✔️ 要求之子字串之以零起始的起始字元位置。 如果負數,則會從來源字串結尾擷取子字串。
length (長度) int 子字串中要求的字元數。 默認行為是從 startingIndex來源 字串結尾。

傳回

指定字串中的子字串。 子字串開始於 startingIndex (以零為基礎的) 字元位置,並延續到字串結尾或長度字元 (如果有指定)。

範例

substring("123456", 1)        // 23456
substring("123456", 2, 2)     // 34
substring("ABCD", 0, 2)       // AB
substring("123456", -2, 2)    // 56