In JS,slice(), substring() and substr() are used to intercept strings. What are the differences? If you have doubts, this article may help you.
A, the substring ()
The substring() method returns a string between one index and another with the following syntax:
str.substring(indexStart, [indexEnd])
Here are six points to note:
- Substring () is reachable from the extracted character indexStart but does not include indexEnd
- If indexStart equals indexEnd, subString () returns an empty string.
- If indexEnd is omitted, the substring() character is extracted to the end of the string.
- If either argument is less than 0 or NaN, it is treated as 0.
- If either parameter is greater than stringname. length, it is treated as stringname. length.
- If indexStart is greater than indexEnd, the effect subString () is as if the two arguments were swapped; For example, str.substring(1, 0) == str.substring(0, 1)
Here is some sample code:
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'Copy the code
Second, the substr ()
The substr() method returns the specified number of characters in the string starting at the specified position, with the syntax:
str.substr(start, [length])
Here are four points to note:
substr()
fromstart
The acquisition length islength
Character (interception stops if it reaches the end of the string).- if
start
Is positive and greater than or equal to the length of the string, thensubstr()
Returns an empty string.- if
start
Is negative, then the value is calculated by adding the length of the string (if the value is still negative after adding the length of the string, the value is truncated from 0).- if
length
Zero or negative,substr()
Returns an empty string. iflength
If omitted, willsubstr()
The character is extracted to the end of the string.
Here is some sample code:
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) :Copy the code
Note that Microsoft’s JScript does not support negative values for initial indexes. If you want to use this feature, you can use the following compatibility code to resolve this error:
// 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);
}Copy the code
Substring () differs from substr()
The arguments to the substring() method represent the starting and ending indexes, and the arguments to the substr() method represent the starting index and the length of characters to be included in the generated string, as shown in the following example:
var text = 'Mozilla';
console.log(text.substring(2.5)); // => "zil"
console.log(text.substr(2.3)); // => "zil"Copy the code
Four, slice ()
The slice() method returns a string between one index and another with the following syntax:
str.slice(beginIndex[, endIndex])
Here are three points to note:
- if
beginIndex
Is negative, then the value is calculated by adding the length of the string (if the value is still negative after adding the length of the string, the value is truncated from 0).- if
beginIndex
Is greater than or equal to the length of the stringslice()
Returns an empty string.- if
endIndex
If omitted, willslice()
The character is extracted to the end of the string. If it’s negative, it’s treated asstrLength + endIndex
Among themstrLength
Is the length of the string.
Here is some sample code:
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)); // '(-1, -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) :Copy the code
Address Wang Yulu’s personal website