start_index
Index from where slice should begin. If value is provided in negative it means start from last. e.g. -1 for last character.
end_index
Index after end of slice. If not provided slice will be taken from start_index to end of string. In case of negative value index will be measured from end of string.
from
It should be a non negative integer to specify index from where sub-string should start.
to
An optional non negative integer to provide index before which sub-string should be finished.
If start > stop, slice() will NOT swap the 2 arguments.
If start is negative: sets char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
If stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.
The one answer is fine but requires a little reading into. Especially with the new terminology "stop".
My Go -- organized by differences to make it useful in addition to the first answer by Daniel above:
1) negative indexes. Substring requires positive indexes and will set a negative index to 0. Slice's negative index means the position from the end of the string.
General comment -- I find it weird that the second index is the position after the last character of the slice or substring. I would expect "1234".slice(2,2) to return "3". This makes Andy's confusion above justified -- I would expect "1234".slice(2, -1) to return "34". Yes, this means I'm new to Javascript. This means also this 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.
The difference between substring and slice - is how they work with negative and overlooking lines abroad arguments:
substring (start, end)
Negative arguments are interpreted as zero. Too large values are truncated to the length of the string:
alert ( "testme" .substring (-2)); // "testme", -2 becomes 0
Furthermore, if start > end, the arguments are interchanged, i.e. 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"
slice
Negative values are measured from the end of the line:
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 is much more convenient than the strange logic substring.
A negative value of the first parameter to substr supported in all browsers except IE8-.
If the choice of one of these three methods, for use in most situations - it will be slice: negative arguments and it maintains and operates most obvious.
For slice(start, stop), if stop is negative, stop will be set to: string.length – Math.abs(stop), rather (string.length – 1) – Math.abs(stop).
The only difference between slice and substring method is of arguments
Both take two arguments e.g. start/from and end/to.
You cannot pass a negative value as first argument for substring method but for slice method to traverse it from end.
Slice method argument details:
REF: http://www.thesstech.com/javascript/string_slice_method
Arguments
start_index Index from where slice should begin. If value is provided in negative it means start from last. e.g. -1 for last character. end_index Index after end of slice. If not provided slice will be taken from start_index to end of string. In case of negative value index will be measured from end of string.
Substring method argument details:
REF: http://www.thesstech.com/javascript/string_substring_method
Arguments
from It should be a non negative integer to specify index from where sub-string should start. to An optional non negative integer to provide index before which sub-string should be finished.
slice()
works likesubstring()
with a few different behaviors.What they have in common:
start
equalsstop
: returns an empty stringstop
is omitted: extracts characters to the end of the stringDistinctions of
substring()
:start > stop
, thensubstring
will swap those 2 arguments.NaN
, it is treated as if it were0
.Distinctions of
slice()
:start > stop
,slice()
will NOT swap the 2 arguments.start
is negative: sets char from the end of string, exactly likesubstr()
in Firefox. This behavior is observed in both Firefox and IE.stop
is negative: sets stop to:string.length – Math.abs(stop)
(original value), except bounded at 0 (thus,Math.max(0, string.length + stop)
) as covered in the ECMA specification.Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()
The one answer is fine but requires a little reading into. Especially with the new terminology "stop".
My Go -- organized by differences to make it useful in addition to the first answer by Daniel above:
1) negative indexes. Substring requires positive indexes and will set a negative index to 0. Slice's negative index means the position from the end of the string.
2) Swapping of indexes. Substring will reorder the indexes to make the first index less than or equal to the second index.
--------------------------
General comment -- I find it weird that the second index is the position after the last character of the slice or substring. I would expect "1234".slice(2,2) to return "3". This makes Andy's confusion above justified -- I would expect "1234".slice(2, -1) to return "34". Yes, this means I'm new to Javascript. This means also this behavior:
My 2c.
The difference between substring and slice - is how they work with negative and overlooking lines abroad arguments:
substring (start, end)
Negative arguments are interpreted as zero. Too large values are truncated to the length of the string: alert ( "testme" .substring (-2)); // "testme", -2 becomes 0
Furthermore, if start > end, the arguments are interchanged, i.e. plot line returns between the start and end:
slice
Negative values are measured from the end of the line:
It is much more convenient than the strange logic substring.
A negative value of the first parameter to substr supported in all browsers except IE8-.
If the choice of one of these three methods, for use in most situations - it will be slice: negative arguments and it maintains and operates most obvious.
Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions:
He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn´t.
Here is his article about this http://www.bennadel.com/blog/2159-using-slice-substring-and-substr-in-javascript.htm