String slicing (slice, substr, substring) JavaScript in

// first define a string 
var str1 = 'Hello World!'

Look at the parameters needed to pass

slice(start,end)

substr(start,length)

subtring(start,stop)

Note: Because the string type is immutable, so the return value of the new string are three methods

It can be seen that the first parameter is the start, so

When a parameter passed
when the parameter is not a non-negative or pass parameters, return results are the same

console.log(str1.slice(4))        //  o World!
console.log(str1.substr(4))        //  o World!
console.log(str1.substring(4))        //  o World!

console.log(str1.slice(0))        //  Hello World!
console.log(str1.substr(0))        //  Hello World!
console.log(str1.substring(0))        //  Hello World!


console.log(str1.slice())        //  Hello World!
console.log(str1.substr())        //  Hello World!
console.log(str1.substring())        //  Hello World!

Note that, if this length is equal to the string parameter if more than before, is an empty string is returned

console.log(str1.slice(12))      //  ""
console.log(str1.substr(12))      //  ""
console.log(str1.substring(12))      //  ""

 When parameters are passed as negative:

console.log(str1.slice(-4))      //  rld!
console.log(str1.substr(-4))      //  rld!
console.log(str1.substring(-4))      //  Hello World!
console.log(str1.substri ng(-6))      //  Hello World!


console.log(str1.slice(-12))      //  Hello World!
console.log(str1.substr(-12))      //  Hello World!
console.log(str1.substring(-12))      //  Hello World!

As can be seen from the above example, if the absolute value of the negative value of the parameter is greater than or equal to the pass length of the string, the full string is returned; the absolute value of a negative value if the passed parameter is less than the length of the string, slice () and substr () method will be based on the value of the size, length of the string taken flashback, while for the substring (), regardless of what the incoming negative parameters are returned full string.

 

When two arguments

slice (start, end) are based on the passed parameter index, as follows:

// Parameter same symbols, if the second parameter is less than equal to the first parameter, returns the empty string, such as: 
the console.log (str1.slice (-1, -4))       //   "" 
the console.log ( str1.slice (. 5,. 3))       //   "" 
// second parameter is zero, the first parameter is greater than or equal to the length of the string, the second parameter or length less negative (such as length 12, the second argument is less than equal to -12), it returns a null character is 
the console.log (str1.slice (. 3, 0))       //   "" 
the console.log (str1.slice (-5, 0))       //   "" 
the console.log (str1.slice (12 is, 15))       //   "" 
the console.log (str1.slice (-15, -12))       //   "" 
//   second parameter is greater than a first parameter, interception elements of their range, such as: 
console.log (str1.slice (-4, -1))       //   "RLD" 
Console.      log(str1.slice(3, 5))      //   "LO" 


// Parameter different signs, positive parameters constant negative length parameter calculation and also for comparing the second parameter to the first parameter, for example: 
the console.log (str1.slice (. 3 , -4))       // corresponds str1.slice (. 3,. 8) 
the console.log (str1.slice (-5,. 11))       // corresponds str1.slice (. 7,. 11) 
the console.log (str1.slice (6, -11))       // corresponds str1.slice (6, 1)

The first parameter substr (start, length) is the index, the second parameter is taken to be the length of

// If the second parameter is non-positive, then must return a null string 
the console.log (str1.substr (. 4, -2))       //   "" 
the console.log (str1.substr (. 3, -14))       //   "" 
console.log (str1.substr (-4, 0))       //   "" 
console.log (str1.substr (5, 0))       //   "" 
// If the first argument is negative, the length of the parameter string, and a starting index, the interception of the second length parameter, until the length of the intercept over 
the console.log (str1.substr (-4,. 3))       //   corresponds str1.substr ( 8,3) is the 'RLD' 
the console.log (str1.substr (-3,. 5))       //   corresponds str1.substr (9, 5) is the 'ld!'

substring (start, stop) are two parameters subscript, then the difference between this method and the slice (start, end) in which it?

// passed argument is positive, returns a value of two methods is the same, such as: 
the console.log (str1.slice (. 4))       //   'O World!' 
The console.log (str1.substring (. 4))       //   'O World!' 
the console.log (str1.slice (. 3,. 6))       //   'LO' 
the console.log (str1.substring (. 3,. 6))       //   'LO' 

// in passed when the parameter is negative, substring will be converted into a negative argument 0, such as: 
the console.log (str1.substring (-3))       // corresponds str1.substring (0) is the 'the Hello World!' 

/ / If there is a two parameter is negative, then, will be converted to the negative argument 0, and the parameter as the initial position, the other non-negative parameter as a termination position, such as: 
the console.log (str1. the substring (-4,. 5))       //   corresponds str1.substring (0, 5) is, 'the Hello' 
the console.log (str1.substring (. 5,      -6))      //   corresponds str1.substring (0, 5) is the 'Hello'

 

 

References:

JS and substr (), substring (), slice () distinction  https://blog.csdn.net/u013270347/article/details/80751874

subString JavaScript class of String () method and the slice () method  https://www.easck.com/cos/2019/0615/303661.shtml

Guess you like

Origin www.cnblogs.com/yungiu/p/11497946.html