Home >Web Front-end >JS Tutorial >The difference between slice, SubString and SubStr in javascript

The difference between slice, SubString and SubStr in javascript

yulia
yuliaOriginal
2018-09-08 16:34:041715browse


slice() Definition and Usage
slice() method returns selected elements from an existing array.
string.slice(start, end) extracts a string
string.substring(start, end) extracts a string, end does not support negative numbers
string.substr(start, len) extracts a length of The string of len

1, slice and substring receive the starting position and the end position (excluding the end position), while substr receives the starting position and the length of the string to be returned. Look directly at the following example:

 var test = 'hello world';
 alert(test.slice(4,7));             //o w
 alert(test.substring(4,7));         //o w
 alert(test.substr(4,7));            //o world\

2. Substring uses the smaller of the two parameters as the starting position and the larger parameter as the ending position. For example:

 alert(test.substring(7,4)); //o w

3. When the received parameter is a negative number, slice will add the length of its string to the corresponding negative number, and the result will be used as the parameter; substr will only add the first parameter The result added to the length of the string is used as the first parameter; substring simply converts all negative parameters directly to 0. The test code is as follows:

var test = 'hello world';
alert(test.slice(-3));         //rld
alert(test.substring(-3));     //hello world
alert(test.substr(-3));        //rld
alert(test.slice(3,-4));       //lo w
alert(test.substring(3,-4));   //hel
alert(test.substr(3,-4));      //空字符串

Definition and usage

The slice() method returns selected elements from an existing array.
Syntax: arrayObject.slice(start,end)
start: required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second to last element, and so on.
end:optional. Specifies where the selection ends. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to the end of the array. If this parameter is negative, it specifies the elements starting from the end of the array.
Return value: Returns a new array containing the elements in arrayObject from start to end (excluding this element).
Note: Please note that this method does not modify the array, but returns a subarray. If you want to delete a segment of elements from an array, you should use the method Array.splice().

Note: You can use negative values ​​to select elements from the tail of the array.
If end is not specified, the slice() method will select all elements from start to the end of the array.

Example: In this example, we will create a new array and then display the elements selected from it:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr + "<br />")
document.write(arr.slice(1) + "<br />")
document.write(arr)
</script>

Output:
George,John,Thomas
John, Thomas
George,John,Thomas

The above is the detailed content of The difference between slice, SubString and SubStr in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn