What is the difference between String.slice and String.substring?
有谁知道这两种方法有何区别:
String.slice
String.substring
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 string stop
is omitted: extracts characters to the end of the string Distinctions 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 NOT swap the 2 arguments. 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). Source: Rudimentary Art of Programming & Development: Javascript: substr() vs substring()
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'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()==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()
.
Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions:
String.slice( begin [, end ] )
String.substring( from [, to ] )
String.substr( start [, length ] )
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
链接地址: http://www.djcxy.com/p/2976.html上一篇: Bower和npm有何区别?