javascript - Returns the wrong character in SubString method -
i new js, have tried below code,
var str = "\">2\"" var res = str.substring(2,1);
return: >
expected: 2
i have checked below code,
var str = "\">2\"" var res = str.substring(2);
return: "2\"
expected: "2\"
please let me know if misunderstand anything, why returns >
instead of 2
. in c# works correctly.
thanks in advance
actually doing incorrect in case 1
when
str.substring(2,1);
that means
str.substring(1,2);
from docs
if indexstart greater indexend, effect of substring() if 2 arguments swapped; example, str.substring(1, 0) == str.substring(0, 1).
since char @ 1,2 >
, getting same.
however when
str.substring(2);
it equals
str.substring(2, str.length-1);
if indexend omitted, substring() extracts characters end of string.
Comments
Post a Comment