Differences between substr and substring in js
Someone asked the two differences in the group today. I will take this opportunity to list them here.
Substring (from,)
Start and end locations, index starting from scratch
Parameter description
From is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.
The substring returned by the substring method includes the characters at start, but not the characters at.
If from is equal to, this method returns an empty string (a string with a length of 0 ).
If the from parameter is greater than the to parameter, the two parameters are exchanged before the sub-string is extracted.
If from or to is a negative number, it is replaced with 0.
Substr (from, length)
Start is required. The starting position of the required substring. The index of the first character in the string is 0.
Length is optional. The number of characters to be included in the returned substring.
var str = 0123456789;alert(str.substring(0));------------0123456789alert(str.substring(5));------------56789alert(str.substring(10));-----------alert(str.substring(12));-----------alert(str.substring(-5));-----------0123456789alert(str.substring(-10));----------0123456789alert(str.substring(-12));----------0123456789alert(str.substring(0,5));----------01234alert(str.substring(0,10));---------0123456789alert(str.substring(0,12));---------0123456789alert(str.substring(2,0));----------01alert(str.substring(2,2));----------alert(str.substring(2,5));----------234alert(str.substring(2,12));---------23456789alert(str.substring(2,-2));---------01alert(str.substring(-1,5));---------01234alert(str.substring(-1,-5));--------alert(str.substr(0));---------------0123456789alert(str.substr(5));---------------56789alert(str.substr(10));--------------alert(str.substr(12));--------------alert(str.substr(-5));--------------56789alert(str.substr(-10));-------------0123456789alert(str.substr(-12));-------------0123456789alert(str.substr(0,5));-------------01234alert(str.substr(0,10));------------0123456789alert(str.substr(0,12));------------0123456789alert(str.substr(2,0));-------------alert(str.substr(2,2));-------------23alert(str.substr(2,5));-------------23456alert(str.substr(2,12));------------23456789alert(str.substr(2,-2));------------alert(str.substr(-1,5));------------9alert(str.substr(-1,-5));-----------