Coder Perfect

What’s the difference between String.slice and String.substring, and how do you use them?


Is anyone familiar with the differences between these two approaches?


Asked by tmim

Solution #1

slice() is similar to substring() but has a few additional features.

Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);

They all have one thing in common:

Distinctions of substring():

Distinctions of slice():

The Primitive Art of Programming and Development: substring vs. substr() in Javascript ()

Answered by Daniel Vassallo

Solution #2

Otherwise, continue reading for a complete comparison.

Note #1: slice()==substring()

Note #2: slice()==substring()

Note #3: slice()==substr()

Note #4: slice()==substr()==substring()

Note #5: slice()==substring()

Note #6: slice()==substr()==substring()

So, there’s a distinction between slice() and substr(), although substring() is essentially a copy of slice() ().

Answered by Waddah

Solution #3

Ben Nadel has produced a decent post about this, pointing out the differences in these functions’ parameters:

String.slice( begin [, end ] )
String.substring( from [, to ] )
String.substr( start [, length ] )

He also reminds out that if the slice parameters are negative, the string is referenced from the end. Substring and substr don’t work together.

This is the link to his article on the subject.

Answered by Jón Viðar Þorsteinsson

Solution #4

The one solution is adequate, but it necessitates some investigation. Especially with the new “halt” wording.

In addition to Daniel’s original solution, I’ve structured my Go by differences to make it more useful:

1) Indexes with negative values. Positive indexes are required by Substring, and a negative index will be set to 0. Slice’s negative index means the position from the end of the string.

"1234".substring(-2, -1) == "1234".substring(0,0) == ""
"1234".slice(-2, -1) == "1234".slice(2, 3) == "3"

2) Indexes are swapped. The indexes will be reordered so that the first index is less than or equal to the second index.

"1234".substring(3,2) == "1234".substring(2,3) == "3"
"1234".slice(3,2) == ""

General remark: I find it odd that the second index is the place after the slice or substring’s last character. “1234”.slice(2,2) should return “3,” right? This clarifies Andy’s earlier confusion: I would expect “1234”.slice(2, -1) to produce “34.” Yes, I’m a novice when it comes to Javascript. This includes the following behavior:

"1234".slice(-2, -2) == "", "1234".slice(-2, -1) == "3", "1234".slice(-2, -0) == "" <-- you have to use length or omit the argument to get the 4.
"1234".slice(3, -2) == "", "1234".slice(3, -1) == "", "1234".slice(3, -0) == "" <-- same issue, but seems weirder.

My 2c.

Answered by Gerard ONeill

Solution #5

The distinction between substring and slice is how they handle negative and omitted lines in foreign arguments:

Negative arguments are treated as though they were zero. Values that are too long are truncated to the length of the string:

alert("testme".substring(-2)); // "testme", -2 becomes 0

Additionally, if start > end, the arguments are swapped, resulting in plot line returns between the start and end:

alert("testme".substring(4, -1)); // "test"
// -1 Becomes 0 -> got substring (4, 0)
// 4> 0, so that the arguments are swapped -> substring (0, 4) = "test"

Negative values are calculated from the line’s end:

alert("testme".slice(-2)); // "me", from the end position 2
alert("testme".slice(1, -1)); // "estm", from the first position to the one at the end.

It’s a lot easier to use than the strange logic substring.

All browsers except IE8- allow a negative value for the first parameter to substr.

If you had to pick one of these three ways to utilize in most instances, it would be slice: negative arguments because it is the most obvious and maintains.

Answered by Alexandr

Post is based on