2\"" var res = str.substring(2,1); Re...">

Returns invalid character in SubString method

I'm new to JS, I tried the code below,

var str = "\">2\""
var res = str.substring(2,1);

Return: >

Expected: 2

I checked this below code,

var str = "\">2\""
var res = str.substring(2);

Return: "2\"

Expected: "2\"

Please let me know if I misunderstand why it returns >instead 2. In C #, it works correctly.

Thanks in advance

+4
source share
3 answers

In fact, you are doing wrong with her in case 1

When you do

str.substring(2,1);

It means that

str.substring(1,2);

From docs

If indexStart is larger than indexEnd, then the substring () effect looks as if both arguments were replaced; e.g. str.substring (1, 0) == str.substring (0, 1).

char 1,2 - >, .

,

str.substring(2);

 str.substring(2, str.length-1);

indexEnd , substring() .

+6

javascript

string.substring(, )

var str = "\">2\""
var res = str.substring(2,1);

"start" , "end", :

var str = "\">2\""
var res = str.substring(2);

2 :

2 "

javascript

0

(2,1) 2- , (2) .

0

Source: https://habr.com/ru/post/1682209/


All Articles