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

在JS中,slice()、substring()、substr()都有截取字符串的做用,那他們有哪些用法上的區別呢?若是你也有疑惑,這篇文章或許可以幫助到你。

1、substring()

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

str.substring(indexStart, [indexEnd])java

下面有六點須要注意:web

  • substring()從提取的字符indexStart可達但不包括 indexEnd
  • 若是indexStart 等於indexEnd,substring()返回一個空字符串。
  • 若是indexEnd省略,則將substring()字符提取到字符串的末尾。
  • 若是任一參數小於0或是NaN,它被視爲爲0。
  • 若是任何一個參數都大於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'複製代碼

2、substr()

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

str.substr(start, [length])this

下面有四點須要注意:spa

  • substr()會從start獲取長度爲length字符(若是截取到字符串的末尾,則會中止截取)。
  • 若是start是正的而且大於或等於字符串的長度,則substr()返回一個空字符串。
  • start爲負數,則將該值加上字符串長度後再進行計算(若是加上字符串的長度後仍是負數,則從0開始截取)。
  • 若是length爲0或爲負數,substr()返回一個空字符串。若是length省略,則將substr()字符提取到字符串的末尾。

如下是一些示例代碼:prototype

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不支持起始索引的負值。若是要使用此功能,可使用如下兼容性代碼來解決此錯誤:code

// 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);
}複製代碼

3、substring()與substr()的主要區別

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

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

4、slice()

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

str.slice(beginIndex[, endIndex])

下面有三點須要注意:

  • beginIndex爲負數,則將該值加上字符串長度後再進行計算(若是加上字符串的長度後仍是負數,則從0開始截取)。
  • 若是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
原文地址王玉路的我的網站

相關文章
相關標籤/搜索