JS字符串截取函數(shù)slice(),substring(),substr()的區(qū)別

在JS中,slice()、substring()、substr()都有截取字符串的作用,那他們有哪些用法上的區(qū)別呢?如果你也有疑惑,這篇文章或許能夠幫助到你。

一、substring()

substring()方法返回一個索引和另一個索引之間的字符串,語法如下:

str.substring(indexStart, [indexEnd])

下面有六點需要注意:

  • substring()從提取的字符indexStart可達但不包括 indexEnd
  • 如果indexStart 等于indexEnd,substring()返回一個空字符串。
  • 如果indexEnd省略,則將substring()字符提取到字符串的末尾。
  • 如果任一參數(shù)小于0或是NaN,它被視為為0。
  • 如果任何一個參數(shù)都大于stringName.length,則被視為是stringName.length。
  • 如果indexStart大于indexEnd,那么效果substring()就好像這兩個論點被交換了一樣; 例如,str.substring(1, 0) == str.substring(0, 1)

以下是一些示例代碼:

var str = 'abcdefghij';
console.log('(1, 2): '   + str.substring(1, 2));   // '(1, 2): b'
console.log('(1, 1): '   + str.substring(1, 1));   // '(1, 1): '
console.log('(-3, 2): '  + str.substring(-3, 2));  // '(-3, 2): ab'
console.log('(-3): '     + str.substring(-3));     // '(-3): abcdefghij'
console.log('(1): '      + str.substring(1));      // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substring(-20, 2)); // '(-20, 2): ab'
console.log('(2, 20): '  + str.substring(2, 20));  // '(2, 20): cdefghij'
console.log('(20, 2): '  + str.substring(20, 2));  // '(20, 2): cdefghij'

二、substr()

substr()方法返回從指定位置開始的字符串中指定字符數(shù)的字符,語法如下:

str.substr(start, [length])

下面有四點需要注意:

  • substr()會從start獲取長度為length字符(如果截取到字符串的末尾,則會停止截取)。
  • 如果start是正的并且大于或等于字符串的長度,則substr()返回一個空字符串。
  • start為負數(shù),則將該值加上字符串長度后再進行計算(如果加上字符串的長度后還是負數(shù),則從0開始截?。?。
  • 如果length為0或為負數(shù),substr()返回一個空字符串。如果length省略,則將substr()字符提取到字符串的末尾。

以下是一些示例代碼:

var str = 'abcdefghij';
console.log('(1, 2): '   + str.substr(1, 2));   // '(1, 2): bc'
console.log('(-3, 2): '  + str.substr(-3, 2));  // '(-3, 2): hi'
console.log('(-3): '     + str.substr(-3));     // '(-3): hij'
console.log('(1): '      + str.substr(1));      // '(1): bcdefghij'
console.log('(-20, 2): ' + str.substr(-20, 2)); // '(-20, 2): ab'
console.log('(20, 2): '  + str.substr(20, 2));  // '(20, 2): '

需要注意的是,Microsoft的JScript不支持起始索引的負值。如果要使用此功能,可以使用以下兼容性代碼來解決此錯誤:

// only run when the substr() function is broken
if ('ab'.substr(-1) != 'b') {
  /**
   *  Get the substring of a string
   *  @param  {integer}  start   where to start the substring
   *  @param  {integer}  length  how many characters to return
   *  @return {string}
   */
  String.prototype.substr = function(substr) {
    return function(start, length) {
      // call the original method
      return substr.call(this,
        // did we get a negative start, calculate how much it is from the beginning of the string
        // adjust the start parameter for negative value
        start < 0 ? this.length + start : start,
        length)
    }
  }(String.prototype.substr);
}

三、substring()與substr()的主要區(qū)別

substring()方法的參數(shù)表示起始和結(jié)束索引,substr()方法的參數(shù)表示起始索引和要包含在生成的字符串中的字符的長度,示例如下:

var text = 'Mozilla';
console.log(text.substring(2,5)); // => "zil"
console.log(text.substr(2,3)); // => "zil"

四、slice()

slice()方法返回一個索引和另一個索引之間的字符串,語法如下:

str.slice(beginIndex[, endIndex])

下面有三點需要注意:

  • beginIndex為負數(shù),則將該值加上字符串長度后再進行計算(如果加上字符串的長度后還是負數(shù),則從0開始截?。?/li>
  • 如果beginIndex大于或等于字符串的長度,則slice()返回一個空字符串。
  • 如果endIndex省略,則將slice()字符提取到字符串的末尾。如果為負,它被視為strLength + endIndex其中strLength是字符串的長度。

以下是一些示例代碼:

var str = 'abcdefghij';
console.log('(1, 2): '   + str.slice(1, 2));   // '(1, 2): b'
console.log('(-3, 2): '  + str.slice(-3, 2));  // '(-3, 2): '
console.log('(-3, 9): '  + str.slice(-3, 9));  // '(-3, 9): hi'
console.log('(-3): '     + str.slice(-3));     // '(-3): hij'
console.log('(-3,-1): ' + str.slice(-3,-1));     // '(-3,-1): hi'
console.log('(0,-1): '  + str.slice(0,-1));     // '(0,-1): abcdefghi'
console.log('(1): '      + str.slice(1));      // '(1): bcdefghij'
console.log('(-20, 2): ' + str.slice(-20, 2)); // '(-20, 2): ab'
console.log('(20): '     + str.slice(20));  // '(20): '
console.log('(20, 2): '  + str.slice(20, 2));  // '(20, 2): '

參考文檔:MDN web docs
原文地址:王玉路的個人網(wǎng)站

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容