What is the difference between substr and substrin

2019-01-01 16:26发布

问题:

What is the difference between

alert(\"abc\".substr(0,2));

and

alert(\"abc\".substring(0,2));

They both seem to output “ab”.

回答1:

The difference is in the second argument. The second argument to substring is the index to stop at (but not include), but the second argument to substr is the maximum length to return.

Links?

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring



回答2:

substr (MDN) takes parameters as (from, length).
substring (MDN) takes parameters as (from, to).

alert(\"abc\".substr(1,2)); // returns \"bc\"
alert(\"abc\".substring(1,2)); // returns \"b\"

You can remember substring takes indices, as does yet another string extraction method, slice.

When starting from 0 you can use either method.



回答3:

As hinted at in yatima2975\'s answer, there is an additional difference:

substr() accepts a negative starting position as an offset from the end of the string. substring() does not.

From MDN:

If start is negative, substr() uses it as a character index from the end of the string.

So to sum up the functional differences:

substring(begin-offset, end-offset-exclusive) where begin-offset is 0 or greater

substr(begin-offset, length) where begin-offset may also be negative



回答4:

Another gotcha I recently came across is that in IE 8, \"abcd\".substr(-1) erroneously returns \"abcd\", whereas Firefox 3.6 returns \"d\" as it should. slice works correctly on both.

More on this topic can be found here.



回答5:

The main difference is that

substr() allows you to specify the maximum length to return

substring() allows you to specify the indices and the second argument is NOT inclusive

There are some additional subtleties between substr() and substring() such as the handling of equal arguments and negative arguments. Also note substring() and slice() are similar but not always the same.

  //*** length vs indices:
    \"string\".substring(2,4);  // \"ri\"   (start, end) indices / second value is NOT inclusive
    \"string\".substr(2,4);     // \"ring\" (start, length) length is the maximum length to return
    \"string\".slice(2,4);      // \"ri\"   (start, end) indices / second value is NOT inclusive

  //*** watch out for substring swap:
    \"string\".substring(3,2);  // \"r\"    (swaps the larger and the smaller number)
    \"string\".substr(3,2);     // \"in\"
    \"string\".slice(3,2);      // \"\"     (just returns \"\")

  //*** negative second argument:
    \"string\".substring(2,-4); // \"st\"   (converts negative numbers to 0, then swaps first and second position)
    \"string\".substr(2,-4);    // \"\"
    \"string\".slice(2,-4);     // \"\"

  //*** negative first argument:
    \"string\".substring(-3);   // \"string\"        
    \"string\".substr(-3);      // \"ing\"  (read from end of string)
    \"string\".slice(-3);       // \"ing\"        


回答6:

The difference is second parameter. Their second parameters, while both numbers, are expecting two different things:

When using substring the second parameter is the first index not to include:

var s = \"string\";
s.substring(1, 3); // would return \'tr\'

var s = \"another example\";
s.substring(3, 7); // would return \'ther\'

When using substr the second parameter is the number of characters to include in the substring:

var s = \"string\";
s.substr(1, 3); // would return \'tri\'

var s = \"another example\";
s.substr(3, 7); // would return \'ther ex\'


回答7:

Slice vs Substr vs Substring vs [ ] Methods

There are performance benefits to each of these javascript methods. Please use these functions accordingly.



回答8:

The big difference is, substr() is a deprecated method that can still be used, but should be used with caution because they are expected to be removed entirely sometime in the future. You should work to remove their use from your code. And the substring() method succeeded and specified the former one.