preface

Today’s code uses array slicing, using slice() without changing the array, using splice() on top of the array, that’s my understanding.

So here’s the question: suddenly two parameters, two parameters negative? Does the second argument represent the length or the stop position? Note that I am very fuzzy to these two methods, use the time need to experiment in the console, or check the corresponding method usage. Next I comb through the four commonly used methods, read clearly.

Method statement

Strsubstring () method

Definition: The substring() method is used to extract the character of a string intermediate between two specified subscripts.

Usage: stringObject. Substring (start, stop)

The return value:

A new string whose value contains a substring of stringObject that contains all characters from start to stop-1 and is stop minus start.

Supplementary notes:

If there is only one argument, start to end, empty string is returned if start is greater than length, start less than 0 equals start at 0. If you have two arguments, you can take a string between them. The substring() method returns a substring that includes characters at start, but not at stop. If the arguments start and stop are equal, the method returns an empty string (that is, a string of length 0). If start is larger than stop, the method swaps these two arguments before extracting the substring, and the method does not change the original string.

Example:

summary

The strsubstring method can only be used to cut strings, not arrays. It does not change the string and returns the result of the cut.

The substr () method

Definition: The substr() method extracts a specified number of characters from the start subscript in a string.

Usage: stringObject. Substr (start, length)

The return value:

A new string containing the length characters from stringObject start (including the character to which start refers). If length is not specified, the string returned contains characters from start to the end of stringObject.

The instance

summary

The substr method, like the strsubstring method, can only be used to slice strings, not arrays. The second value of substr represents the length of the cut, the empty value represents all, and the first value represents the position. The difference is that the value can be negative (length+start). The second part of substr is length, and negative returns an empty string.

Slice () method

Definition: The slice() method returns selected elements from an existing array.

Usage: arrayObject. Slice (start, end)

The return value:

Returns a new array containing elements from the arrayObject from start to end (excluding the element). Can be used with strings that return strings from start to end (excluding the element). This method does not change the original array (or string), and you can use negative values to pick elements from the end of the array.

The instance

summary

Two arguments for slice represent the start and end positions, integers represent positions, and negative numbers can be converted to positive numbers (length+ negative numbers). Then, after conversion, start>stop is used as an empty array or null character.

Splice () method

Definition: The splice() method adds/removes items to/from an array and then returns the deleted items.

Usage: arrayObject. Slice (start, end)

The return value

Returns a new array containing elements from the arrayObject from start to end (excluding the element). This method changes the original array. This method cannot be used for strings.

The instance

Summary:

The splice() method removes zero or more elements starting at index and replaces those deleted elements with one or more values declared in the argument list. If an element is deleted from an arrayObject, an array containing the deleted element is returned. Note that the splice() method works differently from the slice() method, which modifies the array directly. The first parameter represents the position, which can be negative, if negative, can be converted to (length+start), the second parameter is the number, which can be understood as the length, the cut length, can be 0, is negative equals 0, if not, is after. The arguments that follow are the values inserted at that position. Returns the cut array without the cut array (in the case of the third argument, the interpolated array).

summary

This article tries to clarify the differences between the four methods, such as charAt(), which returns a character at a specified position. Now I’ve put together a table just to see the differences between the four methods

The difference between analysis

The method name Used of strings. For array Whether to change the original object The first parameter Second parameter The rest of the parameters Pay attention to the point
substring is no no Start, minus 0, ends with only one argument Stop, negative to zero There is no
substr is no no Start, negative is (length+start), there is only one argument to end Length (c length difference to stop, cutoff position), negative is 0,<=0 return null There is no
slice is is no Start, negative is (length+start), there is only one argument to end Stop, negative is (length+stop), when the converted stop<start, return null There is no
splice no is is Start, negative is (length+start), there is only one argument to end Length, negative is 0,stop<=0, return null Insert value The original array with insert values becomes the new array with cut + insert values

conclusion

For example, the second parameter represents length and stop, which is a little silly. It is also necessary to pay special attention to the case that the two parameters are negative. If you clear up these two points, I believe these methods can be used by you.

And then shamelessly asking for likes!


Thumb up! Thumb up! Thumb up!