Substr() and substring() difference

hi campers,

can anyone explained why
substr() and substring()
count the end of string length differently.

remChar = str => str.substr(1, str.length-2);
console.log(remChar('012345678910');
// output : 1234567891

remChars = str => str.substring(1, str.length-2);
console.log(remChars('012345678910')
 // output : 123456789

thanks in advance

They have different parameters.

Basically, substring is older and harder to use. The second parameter is one beyond the final index desired.

Substr’s second parameter takes the # of characters you want.

thanks for your reply,

basicly substr(startIndex, length)
but why to get the same result as subtring (startIndex, endIndex), i had to change the substr length-3

str.substr(1, str.length-3)
// output : 123456789
str =  "012345678910";

str.substr(1, str.length-2) means
get a string that starts at index 1 and is 10 characters long.

str.substring(1, str.length -2) means
get a string that starts at index 1 and ends at the index right before index 10.
1 Like