Does anyone know what the difference is between these two methods?
String.prototype.slice String.prototype.substring
A big difference with substring() is that if the 1st argument is greater than the 2nd argument, substring() will swap them. slice() returns an empty string if the 1st argument is greater than the 2nd argument.
a substring is a subsequence of a string in which the characters must be drawn from contiguous positions in the string. For example the string CATCGA, the subsequence ATCG is a substring but the subsequence CTCA is not.
The slice() method extracts a part of a string. The slice() method returns the extracted part in a new string. The slice() method does not change the original string. The start and end parameters specifies the part of the string to extract.
slice() The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
slice()
works like substring()
with a few different behaviors.
Syntax: string.slice(start, stop); Syntax: string.substring(start, stop);
What they have in common:
start
equals stop
: returns an empty stringstop
is omitted: extracts characters to the end of the stringDistinctions of substring()
:
start > stop
, then substring
will swap those 2 arguments.NaN
, it is treated as if it were 0
.Distinctions of slice()
:
start > stop
, slice()
will return the empty string. (""
)start
is negative: sets char from the end of string, exactly like substr()
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()
slice()
.substr()
.Otherwise, read on for a full comparison
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note #1: slice()==substring()
slice()
method extracts parts of a string and returns the extracted parts in a new string.substr()
method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.substring()
method extracts parts of a string and returns the extracted parts in a new string.Note #2: slice()==substring()
slice()
Doesn'tsubstr()
Doesn'tsubstring()
Doesn't Note #3: slice()==substring()
slice()
selects characters starting from the end of the stringsubstr()
selects characters starting from the end of the stringsubstring()
Doesn't PerformNote #3: slice()==substr()
slice()
Doesn't Performsubstr()
since the Second Argument is NOT a position, but length value, it will perform as usual, with no problemssubstring()
will swap the two arguments, and perform as usualslice()
Required, indicates: Starting Indexsubstr()
Required, indicates: Starting Indexsubstring()
Required, indicates: Starting IndexNote #4: slice()==substr()==substring()
slice()
Optional, The position (up to, but not including) where to end the extractionsubstr()
Optional, The number of characters to extractsubstring()
Optional, The position (up to, but not including) where to end the extractionNote #5: slice()==substring()
slice()
selects all characters from the start-position to the end of the stringsubstr()
selects all characters from the start-position to the end of the stringsubstring()
selects all characters from the start-position to the end of the stringNote #6: slice()==substr()==substring()
So, you can say that there's a difference between slice()
and substr()
, while substring()
is basically a copy of slice()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With