Home  >  Article  >  Web Front-end  >  What is the difference between the functions slice, splice and split in js

What is the difference between the functions slice, splice and split in js

一个新手
一个新手Original
2017-09-09 10:49:062457browse

I feel so confused every time I encounter these three values. It's hard to tell them apart every time. Don't even know how to use that. Finally, let’s summarize today.

  1. slice()
    Note: This method does not change the original array. Parameters containing headers do not contain unspecified numbers
    arrayObject.slice(start,end);
    You can intercept both strings and arrays. What is returned is a new array.
    slice() method returns selected elements from an existing array.
    Return a new array,. The parameter start is the starting array index of interception, and the end parameter is equal to the position value of the last character you want to retrieve plus 1 (optional) (including header but not ending)
    If the second parameter is omitted, it defaults to the intercepted field From the starting position to the ending position
    For example:

`//如果不传入参数二,那么将从参数一的索引位置开始截取,一直到数组尾
 var a=[1,2,3,4,5,6]; 
 var b=a.slice(0,3);  //[1,2,3] 
 var c=a.slice(3);    //[4,5,6]
//如果两个参数中的任何一个是负数,array.length会和它们相加,试图让它们成为非负数,举例说明: //当只传入一个参数,且是负数时,length会与参数相加,然后再截取 
var a=[1,2,3,4,5,6]; var b=a.slice(-1);  //[6]
//当只传入一个参数,是负数时,并且参数的绝对值大于数组length时,会截取整个数组
var a=[1,2,3,4,5,6];
var b=a.slice(-6);  //[1,2,3,4,5,6]
var c=a.slice(-8);  //[1,2,3,4,5,6]//当传入两个参数一正一负时,length也会先于负数相加后,再截取
var a=[1,2,3,4,5,6];var b=a.slice(2,-3);  //[3]//当传入一个参数,大于length时,将返回一个空数组
var a=[1,2,3,4,5,6];var b=a.slice(6);  //[]//截取一个字符串时var a="i am a boy";
var b=a.slice(0,6);  //"i am a"`

2 splice()
arrayObject.splice(index,howmany,item1,….. ,itemX);
splice() method adds/removes items to/from the array, and then returns the deleted item.
Note: This method will change the original array.
The first and second parameters are required. And it will differ depending on whether the second parameter is 0.
As follows
The first parameter specifies the position to add/delete items from the starting position, and use negative numbers to specify the position from the end of the array.
The second parameter is the number of items to be deleted. If set to 0, the item will not be deleted
1. Delete - used to delete elements, two parameters, the first parameter (the position of the first item to be deleted), the second parameter (the item to be deleted Number).

var lang = ['php', 'java', 'javascript'];  
var removed = lang.splice(1,1); //删除  
console.log(lang); // php, javascript  
console.log(removed); //java

2. Insert - Insert any element into the specified position in the array. Three parameters, the first parameter (the real position), the second parameter (0), and the third parameter (the inserted item).

var insert = lang.splice(0, 0, 'asp'); //从第0个位置开始插入  
console.log(insert); //空数组  
console.log(lang); //asp, php,javascript

3. Replacement - insert any item element into the specified position in the array, and delete any number of items at the same time, three parameters. The first parameter (the starting position), the second parameter (the number of items to delete), and the third parameter (to insert any number of items).

var replace = lang.splice(1,1,"c#","ruby"); //删除一项,插入两项  
console.log(lang);  //console.log(replace); //返回删除的项
split(); 
stringObject.split(separator,howmany)

Note: If an empty string ("") is used as a separator, each character in the stringObject will be split. String.split() does the opposite of what Array.join does.
Used to split a string into a string array. An array of strings. The array is created by splitting the string stringObject into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.
However, if separator is a regular expression that contains subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).
1. If "." is used as a separation, it must be written as follows: String.split("\."), so that it can be separated correctly. String.split(".");
2. If "|" is used as a separator, it must be written as follows: String.split("\|"), so that it can be separated correctly. String.split("|") cannot be used;
3 . If "\" is used as the separation, it must be written as follows: String.split(\), so that it can be separated correctly. String.split("\") cannot be used;
".", "| " and "\" are both escape characters, and "\" must be added;
3. If there are multiple delimiters in a string, you can use "|" as a hyphen, such as: "acount=? and uu =? or n=?", to separate all three, you can use String.split("and|or");

var str="How are you doing today?" document.write(str.split(" ") + "<br />") 
document.write(str.split("") + "<br />") 
document.write(str.split(" ",3)) 

</script>输出: 

How,are,you,doing,today? 
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? 
How,are,you例子 2 在本例中,我们将分割结构更为复杂的字符串: 

"2:3:4:5".split(":")    //将返回["2", "3", "4", "5"] 
"|a|b|c".split("|") //将返回["", "a", "b", "c", ""] 
例子 3 使用下面的代码,可以把句子分割成单词: 

var words = sentence.split(&#39; &#39;)或者使用正则表达式作为 separator: 

var words = sentence.split(/\s+/)例子 4 如果您希望把单词分割为字母,或者把字符串分割为字符,可使用下面的代码: 

"hello".split("")   //可返回 ["h", "e", "l", "l", "o"]若只需要返回一部分字符,请使用 howmany 参数: 

"hello".split("", 3)    //可返回 ["h", "e", "l"]

The above is the detailed content of What is the difference between the functions slice, splice and split in js. 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