The difference between substring(), substr(), slice() in javascript

1.stringObject.substring(start,stop)  is used to extract characters between two specified subscripts in the string.

(1) start is required. A non-negative integer specifying the position within stringObject of the first character of the substring to extract.

(2) stop is optional. A non-negative integer that is one more than the position in stringObject of the last character of the substring to be extracted. If this parameter is omitted, the returned substring will continue until the end of the string.

(3) start starts from 0 to stop (excluding stop) and does not accept negative parameters.

2.stringObject.substr(start,length)  can extract the specified number of characters starting from the start subscript in the string

(1) start Required. The starting subscript of the substring to extract. Must be a numeric value. If negative, the parameter specifies the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second-to-last character, and so on.

(2) length is optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of stringObject is returned.

3.stringObject.slice(start,end) extracts a part of the string and returns the extracted part as a new string

(1) start Required. The starting subscript of the segment to extract. If negative, the parameter specifies the position from the end of the string. That is, -1 refers to the last character of the string, -2 refers to the second-to-last character, and so on.

(2) end is optional. Subscript immediately following the end of the segment to extract. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.

example:

(1) var stringValue = “hello world”;
     alert(stringValue.slice(3));          //”lo world”
     alert(stringValue.substring(3));      //”lo world”
     alert(stringValue.substr(3));        //”lo world”
     alert(stringValue.slice(3,7));         //”lo w”
     alert(stringValue.substring(3,7));    //”lo w”
     alert(stringValue.substr(3,7));       //”lo worl”

Analysis: The returned new string includes all characters from the start (including start) to the end (excluding end) of the string stringObject; if the three have only one parameter n, the remaining strings will be returned from the nth position (Counting the position from 0)
If there are two parameters n, m, slice and substring will return the string from the nth position to the mth position (excluding the mth position), and substr will return from the nth position. m characters starting at position.

(2) var stringValue = “hello world”;

     alert(stringValue.slice(-3)); //”rld”

     alert(stringValue.substring(-3)); //”hello world”

     alert(stringValue.substr(-3)); //”rld”

     alert(stringValue.slice(3,-4)); //”lo w”

     alert(stringValue.substring(3,-4)); //”hel”

     alert(stringValue.substr(3,-4)); //""(empty string)

Analysis: When the parameter is a negative value, slice will add the incoming negative value to the string length (string.length), substr will add the negative first parameter to the string length, and convert the second to 0. substring will convert all negative values ​​to 0.

       IE's JavaScript implementation has a problem handling the case of passing a negative value to the substr() method, which returns the original string.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325804188&siteId=291194637