Note: if you're in a hurry, and/or looking for short answer scroll to the bottom of the answer, and read the last two lines.if Not in a hurry read the whole thing.
What it does?
The slice() method extracts parts of a string and returns the extracted parts in a new string.
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
The substring() method extracts parts of a string and returns the extracted parts in a new string.
Note #2:slice()==substring()
Changes the Original String? slice() Doesn't substr() Doesn't substring() Doesn't
Note #3:slice()==substring()
Using Negative Numbers as an Argument: slice() selects characters starting from the end of the string substr()selects characters starting from the end of the string substring() Doesn't Perform
Note #3:slice()==substr()
if the First Argument is Greater than the Second: slice() Doesn't Perform substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems substring() will swap the two arguments, and perform as usual
the First Argument: slice() Required, indicates: Starting Index substr() Required, indicates: Starting Index substring() Required, indicates: Starting Index
Note #4:slice()==substr()==substring()
the Second Argument: slice() Optional, The position (up to, but not including) where to end the extraction substr() Optional, The number of characters to extract substring() Optional, The position (up to, but not including) where to end the extraction
Note #5:slice()==substring()
What if the Second Argument is Omitted? slice() selects all characters from the start-position to the end of the string substr() selects all characters from the start-position to the end of the string substring() selects all characters from the start-position to the end of the string
Note #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().
in Summary:
if you know the index(the position) on which you'll stop (but NOT include), Use slice()
if you know the length of characters to be extracted use substr().
Note: if you're in a hurry, and/or looking for short answer scroll to the bottom of the answer, and read the last two lines.if Not in a hurry read the whole thing.
let me start by stating the facts:
Syntax:
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note #1:
slice()==substring()
What it does?
The
slice()
method extracts parts of a string and returns the extracted parts in a new string.The
substr()
method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.The
substring()
method extracts parts of a string and returns the extracted parts in a new string.Note #2:
slice()==substring()
Changes the Original String?
slice()
Doesn'tsubstr()
Doesn'tsubstring()
Doesn'tNote #3:
slice()==substring()
Using Negative Numbers as an Argument:
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()
if the First Argument is Greater than the Second:
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 usualthe First Argument:
slice()
Required, indicates: Starting Indexsubstr()
Required, indicates: Starting Indexsubstring()
Required, indicates: Starting IndexNote #4:
slice()==substr()==substring()
the Second Argument:
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()
What if the Second Argument is Omitted?
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()
andsubstr()
, whilesubstring()
is basically a copy ofslice()
.in Summary:
if you know the index(the position) on which you'll stop (but NOT include), Use
slice()
if you know the length of characters to be extracted use
substr()
.