js difference in substr and substring

substr and substring is taken neutron string string, very similar, easily confused, there may be one or two parameters.

Syntax: String .substr ( Start , length index of the first character) is 0, start Required length optional

   String .substring ( Start , length index of the first character) is 0, start end Optional Required

The same point : When there is a parameter, both functions are the same, to return from the start position until the specified end of the string substring

var str = "hello Tony";

str.substr(6);  //Tony

str.substring(6); //Tony

Different points : There are two time parameters

(1) substr (start, length ) is returned from the start position start length the length of the sub-string

 “goodboy”.substr(1,6); //oodboy 

 

[Note] When the length is zero or negative, returns an empty string.

  "goodboy".substr(1,0);   //""

 

 If it is negative, then the parameter declaration from the end of the string since the beginning of the position. That is, the value of -1 means the last character in the string, -2 means penultimate characters, and so on.

"goodboy".substr(-1)  // "y"

 

(2) substring (start, end) returns the substring starting from the start position to the end position (end not included)

“goodboy”.substring(1,6);  //oodbo

Note: ECMA and no substr () is normalized, it is not recommended to use substr ()

Guess you like

Origin www.cnblogs.com/smile6542/p/11895550.html