What is the difference between String.slice and String.substring?

This article was translated from: What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods: Does anyone know what the difference is between these two methods :

String.protorype.slice
String.protorype.substring

#1st Floor

Reference: https://stackoom.com/question/9Pii/What is the difference between String-slice and String-substring


#2nd Floor

slice()like Works substring()with A FEW Different Behaviors. slice()works similarly substring(), but with some different behaviors.

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

What they have in common: What do they have in common :

  1. If startequals stop: returns an empty string if startequals stop: returns an empty string
  2. IF stopIS Omitted: The End of Extracts The characters to String If omitted stop: the end of the character string extracted
  3. If either argument is greater than the string's length, the string's length will be used instead. If any argument is greater than the length of the string, the length of the string will be used .

Distinctions of : substring() Difference :substring()

  1. IF start > stop, the then substringby Will swap Those arguments The 2. If start > stop, then substringswap these two parameters.
  2. If either argument is negative or is NaN, it is treated as if it were 0. If any argument is negative or is , it is regarded as .NaN0

Distinctions of : slice() Difference :slice()

  1. IF start > stop, slice()Will return String The empty. If start > stop, slice()returns an empty string. ( "") ( "")
  2. IF startIS negative: The End sets of char from String, exactly like substr()in Firefox. If startnegative: the end of the string is provided char, and Firefox the substr()same. This behavior is observed in both Firefox and IE. This behavior is observed in both Firefox and IE .
  3. If stopis 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 . If stopnegative: set stop to: string.length – Math.abs(stop)(original value), except for 0 as the boundary (hence, Math.max(0, string.length + stop)), As described in the ECMA specification .

Source: Rudimentary Art of Programming & Development: Javascript: substr () vs substring () Source: Basic Art of Programming and Development: Javascript: substr () vs substring ()


#3rd floor

The one answer is fine but requires a little reading into. The answer is fine but requires a little reading . Especially with the new terminology "stop". Especially with the new terminology "stop ".

My Go-organized by differences to make it useful in addition to the first answer by Daniel above: My go-organized by differences, in addition to Daniel's first answer above, it is also useful:

1) negative indexes. 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. Substring indexes require a positive and negative index is set to 0. negative index indicates a slice from the end of the string s position.

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

2) Swapping of indexes. 2) Swapping of indexes. Substring will reorder the indexes to make the first index less than or equal to the second index. 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. General comment-I found that the second index is the position after the last character of the slice or substring , I think Very strange. I would expect "1234". Slice (2,2) to return "3". 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". This makes Andy's confusion above reasonable-I hope "1234". Slice (2, -1) Return "34". Yes, this means I'm new to Javascript. Yes, this means I am new to Javascript . This means also this behavior: 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. My 2c.


#4th floor

Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions: Ben Nadel has written a good article about this, he pointed out the difference in parameters of 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. He also pointed out that if the parameters to be sliced ​​are negative, they will refer to the string from the end. Substring and substr doesn´t. Substring and substr does not.

The this IS the About His-Article This article was here Wallpaper http://www.bennadel.com/blog/2159-using-slice-substring-and-substr-in-javascript.htm it was his article about this article http: // www. bennadel.com/blog/2159-using-slice-substring-and-substr-in-javascript.htm


#5th Floor

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. Note: If you are in a hurry , and // Find short answers scroll to the bottom of the answer and read the last two lines. If not rush to read the whole thing.


let me start by stating the facts: first let me state the facts:

Syntax: Syntax:
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note # 1: slice()==substring() 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 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. The method extracts a part of the string from 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. The method extracts parts of a string and returns the extracted parts in a new string substring().
Note # 2: slice()==substring() Note # 2:slice()==substring()

Changes the Original String ?
slice()Doesn't slice()No
substr()Doesn't substr()No
substring()Doesn't substring()No
Note # 3: slice()==substring() Note # 3:slice()==substring()

Using Negative Numbers as an Argument: use negative numbers as parameters:
slice()selects characters starting from the end of the string slice()selects
substr()characters starting from the end of the string substr()selects characters starting from the end of the string selects characters starting from the end of the string
substring()Doesn't Perform substring()Do not execute
Note # 3: slice()==substr() Note # 3:slice()==substr()

if the First Argument is Greater than the Second: if the first parameter is greater than the second argument:
slice()Does Not the Perform slice()not performed
substr()since the Second Argument is NOT a position , but length value, it will perform as usual, with no problems substr()because The second parameter is not a position, but a length value, it will be executed as usual, there
substring()will be no problems will swap the two arguments, and perform as usual substring()will exchange the two parameters and execute as usual

the First Argument: First argument:
slice()Required, indicates: Starting Index is slice()required,
substr()indicates: Starting Index is substr()required
substring(), indicates: Starting Index is substring()required, indicates: Starting Index is required, indicates: Starting Index is required, indicates: Starting Index
Note # 4: slice()==substr()==substring() Note # 4:slice()==substr()==substring()

the Second Argument: The second argument:
slice()Optional, The position (up to, but not including) where to end the extraction is slice()optional, the position of ending extraction (up to but not included)
substr()Optional, The number of characters to extract is substr()optional, to extract the number of characters
substring()optional, the position (up to, but not including) where to end the extraction substring()optionally, the end of the extraction position (up to but not including)
Note # 5: slice()==substring() Note # 5:slice()==substring()

What if the Second Argument is Omitted? What if the second argument is omitted?
slice()selects all characters from the start-position to the end of the string slice()select all characters from the starting position to the end of the string
substr()selects all characters from the start-position to the end of the string substr()select from a starting position to a string All characters ending
substring()selects all characters from the start-position to the end of the string substring()select from a substring()starting position to the end of all the characters
Note # 6: slice()==substr()==substring() Note # 6:slice()==substr()==substring()

SO, CAN you that there's the BETWEEN A -difference say slice()and substr(), the while substring()IS Basically A Copy of slice(). So you can say slice()and substr()there are differences between, and substring()are essentially slice()copies.

in Summary: In summary:
if you know the index (the position) on which you'll stop (but NOT include), Use slice() If you know the index (position) to stop (but not include), please use slice()
if you know the length of characters to be extracted use substr(). If you know the length of characters to be extracted use , please use substr().


#6th floor

The difference between substring and slice-is how they work with negative and overlooking lines abroad arguments: how they work with foreign parameters of negative and overlooking lines:

substring (start, end) substring (start, end)

Negative arguments are interpreted as zero. Negative arguments are interpreted as zero . Too large values ​​are truncated to the length of the string: alert ("testme" .substring (-2)); Too large values ​​are truncated to the length of the string : alert ("testme" .substring (-2) ); // "testme", -2 becomes 0 // "testme", -2 becomes 0

Furthermore, if start> end, the arguments are interchanged, ie plot line returns between the start and end: In addition, if start> end, the parameters are interchanged, that is, the drawing line is returned between the start and the 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 slice

Negative values ​​are measured from the end of the line: 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. 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-. The negative value of the first parameter 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. If you choose one of these three methods, use it in most cases-it It will be sliced : a negative parameter, its maintenance and operation are most obvious.

Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105465091