GitHub GitHub: 線上編輯

substring ()

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

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

substring("abcdefg", 1, 2) == "bc"

語法

substring(source, startingIndex [, length])

引數

  • source: 要從中取得子字串的來源字串。
  • startingIndex: 所要求子字串的起始字元位置 (從零開始)。
  • length: 一個選用參數,可用來指定子字串中所要求的字元數。

附註:

startingIndex 可以是負數,在此情況下,將從來源字串結尾擷取子字串。

退貨

來自給定字串的子字串。 子字串從 startingIndex (從零開始) 字元位置開始,並繼續到字串或長度字元 (如果指定的話) 的結尾。

範例

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