本文翻译自: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. 如果选择这三种方法中的一种,在大多数情况下使用 - 它将是切片 :否定参数,它维护和操作最明显。

String.slice和String.substring有什么区别?相关推荐

  1. slice(),splice(),split(),substring(),substr()使用方法和区别

    <一> 1.slice(): Array和String对象都有 在Array中  slice(i,[j]) i为开始截取的索引值,负数代表从末尾算起的索引值,-1为倒数第一个元素 j为结束 ...

  2. 如何从后面截取字符串 String.slice()

    如何从后面截取字符串 String.slice() 有时候我们需要从字符串后面截取字符串,使用 substring() 是不好实现的,可以使用 slice() 如: let str = '第2组第2节 ...

  3. Java中的String,StringBuilder,StringBuffer三者的区别

    最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...

  4. String、StringBuffer、与StringBuilder的区别

    原文:http://www.cnblogs.com/sevenlin/p/sevenlin_StringBuffer_StringBuilder20150806.html String.StringB ...

  5. Java中的string定义的两种方法和区别

    java中的String定义的两种方法和区别 第一种:new方式 String s1 = new String("hello world"); String s2 = new St ...

  6. NULL,,String.Empty三者在C#中的区别

    (1)NULL null 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值 ...

  7. Java基础之String,StringBuilder,StringBuffer三者的区别

    Java基础之String,StringBuilder,StringBuffer三者的区别 目录 运行速度方面 线程安全方面 小结 1. 运行速度方面 运行速度,在这方面运行速度快慢为:StringB ...

  8. String.slice

    String.slice(start, end) start从字符串的哪个index开始截取 默认值0 如果为负值,则从字符串的尾部向前倒推index end到从字符串的哪个index结束截取 默认值 ...

  9. java replaceall函数_JAVA中string.replace和string.replaceAll的区别及用法

    展开全部 JAVA中string.replace()和string.replaceAll()的区别及用法乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),32 ...

最新文章

  1. 2013多校训练赛第三场 总结
  2. rsa python实现_RSA算法python实现
  3. 程序员面试题精选100题(21)-左旋转字符串[算法]
  4. 荣耀v30pro搭载鸿蒙吗,荣耀麒麟30pro+,可以搭载鸿蒙的顶级荣耀,你买了吗?...
  5. 整理你的个人IT资料库
  6. 开机自启动脚本_使用xtu降低笔记本(游戏本)cpu电压及功耗·游戏本延时(以及试着解决开机自启动的问题)...
  7. C#使用CurrentUICulture切换语言
  8. 读书笔记--Head First C#目录
  9. Python控制Word文件中段落格式与文本格式
  10. 看了一些东西,发现一些用css实现一些东西的小技巧就记录下来
  11. [置顶] woff格式字体怎么打开和编辑?
  12. 笔记本进入pe却看不到计算机硬盘,一些笔记本进WINPE后找不到硬盘的解决办法...
  13. php打开文件fopen函数
  14. [UML] 如何找参与者、找用例
  15. DNS 协议是什么?
  16. react04-Ref与Hook
  17. 戒掉坏习惯—六种方法
  18. python 转百分比_编程要从娃娃抓起,北乔治亚大学教授的python编程指南在此
  19. Greenplum数据库故障分析——can not listen port
  20. VBScript参数处理

热门文章

  1. Android AlarmManager 使用指南
  2. Android报错:java.lang.IllegalArgumentException: Surface was abandoned
  3. Android基础--tools:context=.TestActivity作用
  4. linux pwm 调屏_基于嵌入式Linux的LCD背光调节及驱动的实现
  5. (0018)iOS 开发之Github 使用技巧
  6. pwm控制舵机转动角度程序_01 舵机旋转控制基础
  7. CSS中的四种样式及选择器
  8. InetAddress类和InetSocketAddress的使用
  9. 『cURL』curl: (6) Could not resolve host无法解析主机地址
  10. python学习 day2 (3月2日)