String.slice 和 String.substring 有什么区别?
有谁知道这两种方法有什么区别?
String.prototype.slice
String.prototype.substring
Does anyone know what the difference is between these two methods?
String.prototype.slice
String.prototype.substring
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
slice()
的工作方式与substring()
类似,但有一些不同的行为。它们的共同点:
start
等于stop
:返回空字符串stop
:提取字符到字符串末尾区别
substring()
:start >; stop
,然后substring
将交换这两个参数。NaN
,则将其视为0
。区别
slice()
:start > stop
,slice()
将返回空字符串。 (""
)start
为负数:从字符串末尾设置 char,与substr()
完全相同。stop
为负数:将 stop 设置为:string.length – Math.abs(stop)
(原始值),除了以 0 为界(因此,Math. max(0, string.length + stop)
),如 ECMA 规范。来源:编程的基本艺术开发:Javascript:substr() 与 substring()
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 return the empty string. (""
)start
is negative: sets char from the end of string, exactly likesubstr()
.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()
TL;DR:
slice()
。否则,请继续阅读以进行完整比较
语法
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
注意#1:
slice()==substring()
它的作用是什么?
slice()
提取字符串的一部分,并在新字符串中返回提取的部分。substr()
从指定位置的字符开始提取字符串的一部分,并返回指定数量的字符。substring()
提取字符串的一部分并在新字符串中返回提取的部分。注意#2:
slice()==substring()
更改原始字符串?
slice()
不会substr()
不会substring()
不会注意#3:
slice()==substr()==substring()
使用负数作为参数
slice()
选择从字符串末尾开始的字符substr( )
选择从字符串末尾开始的字符substring()
不执行注意#4:
slice()==substr()
如果第一个参数大于第二个,
slice()
不会执行substr()
,因为第二个参数不是位置,而是长度值,它将像往常一样执行,没有任何问题substring()
将交换两个参数,并像往常一样执行第一个参数
slice()
所需的; 需要起始索引substr()
; 需要起始索引substring()
;起始索引注释#5:
slice()==substr()==substring()
第二个参数
slice()
可选;结束提取的位置(最多但不包括)substr()
可选;要提取的字符数substring()
可选;结束提取的位置(最多但不包括)注意#6:
slice()==substring()
如果省略第二个参数怎么办?
slice()
选择从字符串起始位置到结尾的所有字符substr()
选择从字符串起始位置到结尾的所有字符>substring()
选择从字符串起始位置到末尾的所有字符注意#7:
slice()==substr()==substring()< /code>
因此,您可以说
slice()
和substr()
之间存在差异,而substring()
基本上是一个副本slice()
的。如果您想要 substr 的功能:
在不使用已弃用的功能的情况下,您可以这样做:
并获得完全相同的结果,排除所有边缘情况,例如负索引/长度。
TL;DR:
slice()
.substr()
, but that is discouraged as it is deprecated.Otherwise, read on for a full comparison
Syntax
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note #1:
slice()==substring()
What it does?
slice()
extracts parts of a string and returns the extracted parts in a new string.substr()
extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.substring()
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()==substr()==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 #4:
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; starting Indexsubstr()
required; starting Indexsubstring()
required; starting IndexNote #5:
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 #6:
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 #7:
slice()==substr()==substring()
So, you can say that there's a difference between
slice()
andsubstr()
, whilesubstring()
is basically a copy ofslice()
.If you want
substr
's functionality:without using a deprecated feature, you can just do:
And get the exact same results bar all of the edge-cases, like negative index/length.
Ben Nadel 写了一篇关于此的好文章,他指出了这些函数参数的差异:
他还指出,如果切片参数为负数,则它们从末尾引用字符串。 Substring 和 substr 没有。
这是他的文章这。
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.
一个答案很好,但需要一点阅读。尤其是新术语“停止”。
我的Go——按差异组织,使其除了上面丹尼尔的第一个答案之外也有用:
1)负索引。 Substring 需要正索引,并将负索引设置为 0。Slice 的负索引表示距离字符串末尾的位置。
2)交换索引。 Substring 将对索引重新排序,使第一个索引小于或等于第二个索引。
--------------------------
一般评论 -- 我觉得很奇怪,第二个索引是切片或子字符串的最后一个字符之后的位置。我希望“1234”.slice(2,2) 返回“3”。这使得安迪的上述困惑是合理的——我希望“1234”.slice(2, -1) 返回“34”。是的,这意味着我是 Javascript 新手。这也意味着这种行为:
My 2c。
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.
substring
和slice
之间的区别在于它们如何处理负参数和忽略行外部参数:substring(start, end)
负参数被解释为零。太大的值将被截断为字符串的长度:
此外,如果 start > end,参数互换,即绘图线在start和end之间返回:
slice
从行尾开始测量负值:
比奇怪的逻辑
substring<方便多了/代码>。
除 IE8- 之外的所有浏览器都支持 substr 第一个参数的负值。
如果选择这三种方法之一,则适用于大多数情况 - 它将是
slice
:负参数,并且它的维护和操作最为明显。The difference between
substring
andslice
- 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:
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.substr:它允许我们根据指定的索引获取字符串的一部分。
substr 的语法
字符串.substr(开始,结束)
start - 开始索引告诉从哪里开始获取。
end - 结束索引告诉字符串的提取位置。这是可选的。
slice:它提供根据指定索引获取字符串的一部分。它允许我们指定正值和索引。
切片的语法 - string.slice(start,end)
start - 开始索引告诉从哪里开始获取。它是
end - 结束索引告诉字符串的提取位置。这是可选的。
在“拼接”中,开始索引和结束索引都有助于获取正索引和负索引。
字符串中“切片”的示例代码
字符串中“子字符串”的示例代码
[*注意:负索引从字符串末尾开始。]
substr: It's providing us to fetch part of the string based on specified index.
syntax of substr-
string.substr(start,end)
start - start index tells where the fetching start.
end - end index tells upto where string fetches. It's optional.
slice: It's providing to fetch part of the string based on the specified index. It's allows us to specify positive and index.
syntax of slice - string.slice(start,end)
start - start index tells where the fetching start.It's
end - end index tells upto where string fetches. It's optional.
In 'splice' both start and end index helps to take positive and negative index.
sample code for 'slice' in string
sample code for 'substring' in string
[*Note: negative indexing starts at the end of the string.]
slice 和 substring 方法之间的唯一区别是参数。
两者都采用两个参数,例如 start/from 和 end/to。
您不能将负值作为 substring 方法的第一个参数传递,但要让 slice 方法从末尾遍历它。
Slice 方法参数详细信息:
参数
start_index
切片应开始的索引。如果提供负值,则表示从最后一个开始。例如 -1 代表最后一个字符。
结束索引
切片结束后的索引。如果未提供切片,将从 start_index 到字符串末尾进行切片。如果是负值,索引将从字符串末尾开始测量。
子字符串方法参数详细信息:
参数
来自
它应该是一个非负整数来指定子字符串应该开始的索引。
至
一个可选的非负整数,用于提供子字符串应在其之前完成的索引。
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:
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:
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(start, stop)
,如果stop
为负数,stop
将设置为:而不是:
For
slice(start, stop)
, ifstop
is negative,stop
will be set to:rather than: