String.slice和String.substring有什么区别?

本文翻译自:What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods: 有谁知道这两种方法之间的区别是什么:

String.protorype.slice
String.protorype.substring

#1楼

参考:https://stackoom.com/question/9Pii/String-slice和String-substring有什么区别


#2楼

slice() works like substring() with a few different behaviors. slice()工作方式类似于substring() ,但有一些不同的行为。

Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);

What they have in common: 他们有什么共同点:

  1. If start equals stop : returns an empty string 如果start equals stop :返回一个空字符串
  2. If stop is omitted: extracts characters to the end of the string 如果省略stop :将字符提取到字符串的末尾
  3. If either argument is greater than the string's length, the string's length will be used instead. 如果任一参数大于字符串的长度,则将使用字符串的长度。

Distinctions of substring() : substring() 区别

  1. If start > stop , then substring will swap those 2 arguments. 如果start > stop ,那么substring将交换这两个参数。
  2. If either argument is negative or is NaN , it is treated as if it were 0 . 如果任一参数为负数或为NaN ,则将其视为0

Distinctions of slice() : slice() 区别

  1. If start > stop , slice() will return the empty string. 如果start > stopslice()将返回空字符串。 ( "" ) ""
  2. If start is negative: sets char from the end of string, exactly like substr() in Firefox. 如果start为负数:从字符串末尾设置char,与Firefox中的substr()完全相同。 This behavior is observed in both Firefox and IE. 在Firefox和IE中都观察到此行为。
  3. 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 . 如果stop为负:将stop设置为: string.length – Math.abs(stop) (原始值),除了以0为界(因此, Math.max(0, string.length + stop) ),如ECMA规范中所述

Source: Rudimentary Art of Programming & Development: Javascript: substr() vs substring() 来源: 编程与开发的基础艺术:Javascript:substr()vs substring()


#3楼

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. 1)负面指数。 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. 子串需要正索引,并将负索引设置为0.切片的负索引表示从字符串末尾开始的位置。

"1234".substring(-2, -1) == "1234".substring(0,0) == ""
"1234".slice(-2, -1) == "1234".slice(2, 3) == "3"

2) Swapping of indexes. 2)交换索引。 Substring will reorder the indexes to make the first index less than or equal to the second index. 子字符串将重新排序索引以使第一个索引小于或等于第二个索引。

"1234".substring(3,2) == "1234".substring(2,3) == "3"
"1234".slice(3,2) == ""

-------------------------- --------------------------

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". 我希望“1234”.slice(2,2)返回“3”。 This makes Andy's confusion above justified -- I would expect "1234".slice(2, -1) to return "34". 这使得安迪的混淆在上面是合理的 - 我希望“1234”.slice(2,-1)返回“34”。 Yes, this means I'm new to Javascript. 是的,这意味着我是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.

My 2c. 我的2c。


#4楼

Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions: Ben Nadel撰写了一篇关于此的好文章,他指出了这些函数的参数差异:

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.bennadel.com/blog/2159-using-slice-substring-and-substr-in-javascript.htm


#5楼

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() 注意#1: slice()==substring()

What it does? 它能做什么?
The slice() method extracts parts of a string and returns the extracted parts in a new string. slice()方法提取字符串的一部分,并在新字符串中返回提取的部分。
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. substr()方法从指定位置的字符开始提取字符串的一部分,并返回指定数量的字符。
The substring() method extracts parts of a string and returns the extracted parts in a new string. substring()方法提取字符串的一部分,并在新字符串中返回提取的部分。
Note #2: slice()==substring() 注意#2: slice()==substring()

Changes the Original String? 更改原始字符串?
slice() Doesn't slice()没有
substr() Doesn't substr()没有
substring() Doesn't substring()没有
Note #3: slice()==substring() 注意#3: slice()==substring()

Using Negative Numbers as an Argument: 使用负数作为参数:
slice() selects characters starting from the end of the string slice()选择从字符串末尾开始的字符
substr() selects characters starting from the end of the string substr()选择从字符串末尾开始的字符
substring() Doesn't Perform substring()不执行
Note #3: slice()==substr() 注意#3: slice()==substr()

if the First Argument is Greater than the Second: 如果第一个参数大于第二个参数:
slice() Doesn't Perform slice()不执行
substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems substr()因为第二个参数不是位置,而是长度值,它将像往常一样执行,没有任何问题
substring() will swap the two arguments, and perform as usual substring()将交换两个参数,并像往常一样执行

the First Argument: 第一个论点:
slice() Required, indicates: Starting Index slice()必需,表示:开始索引
substr() Required, indicates: Starting Index substr()必需,表示:起始索引
substring() Required, indicates: Starting Index substring()必需,表示:开始索引
Note #4: slice()==substr()==substring() 注意#4: slice()==substr()==substring()

the Second Argument: 第二个论点:
slice() Optional, The position (up to, but not including) where to end the extraction slice()可选,结束提取的位置(最多但不包括)
substr() Optional, The number of characters to extract substr()可选,要提取的字符数
substring() Optional, The position (up to, but not including) where to end the extraction substring()可选,结束提取的位置(最多但不包括)
Note #5: slice()==substring() 注意#5: slice()==substring()

What if the Second Argument is Omitted? 如果第二个论点被省略怎么办?
slice() selects all characters from the start-position to the end of the string slice()选择从字符串的起始位置到结尾的所有字符
substr() selects all characters from the start-position to the end of the string substr()选择从字符串的起始位置到结尾的所有字符
substring() selects all characters from the start-position to the end of the string substring()选择从substring()的起始位置到结尾的所有字符
Note #6: slice()==substr()==substring() 注意#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() . 所以,你可以说slice()substr()之间有区别,而substring()基本上是slice()的副本。

in Summary: 综上所述:
if you know the index(the position) on which you'll stop (but NOT include), Use slice() 如果您知道要停止的索引(位置)(但不包括),请使用slice()
if you know the length of characters to be extracted use substr() . 如果你知道要提取的字符的长度,请使用substr()


#6楼

The difference between substring and slice - is how they work with negative and overlooking lines abroad arguments: 子串和切片之间的区别 - 它们如何与负面和俯视线的国外参数一起工作:

substring (start, end) substring(开始,结束)

Negative arguments are interpreted as zero. 否定参数被解释为零。 Too large values ​​are truncated to the length of the string: alert ( "testme" .substring (-2)); 太大的值被截断为字符串的长度:alert(“testme”.substring(-2)); // "testme", -2 becomes 0 //“testme”,-2变为0

Furthermore, if start > end, the arguments are interchanged, ie plot line returns between the start and end: 此外,如果start> 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-. 除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. 如果选择这三种方法中的一种,在大多数情况下使用 - 它将是切片 :否定参数,它维护和操作最明显。

发布了0 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105465091