slice(), subString(), substr()

String.prototype.slice () is a method of slicing tool js string of a string for 'clipping' operation, without changing the original string.

'helloworld'.slice (0,5); // ' Hello '; 
' helloworld'.slice (. 5); // 'World'; default to the end
 'helloworld'.slice (-5); // ' World '; 10-5 = 5; // first parameter is greater than the second argument is returned null character. 
' helloworld'.slice (5,1); // '' 
'helloworld'.slice (-1, - 5); // ''
 

substring () and slice () is a part can be cut out from the original string inside, but substring () little features.

1. The first parameter is greater than the second parameter, both automatically exchange position;

2. Negative automatically converted to 0;

Two or more of the following effects will be obtained:

'JavaScript'.substring(4, -3); // 'Java'

The principle is: (. 4, 0) => (0,. 4) => 'the Java'

 

Note that, if the same two parameter, the empty string;

'javascript'.slice(2,2); // ''
'javascript'.subsring(2,2); // ''

 

String.prototype.substr () between String.prototype.slice () and String.prototype.substring () between the negative index which will be converted to 0, when the first parameter is not large than the second exchange position.

 

 

 

Guess you like

Origin www.cnblogs.com/aisowe/p/11470404.html