删除字符串方法详细报告

splice()
原型:array
.splice(start[, deleteCount[, item1[, item2[, ...]]]])
下标,删除数量,增加的字符
 1 ExamplesSection
 2 Remove 0 (zero) elements from index 2, and insert "drum"Section
 3 var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
 4 var removed = myFish.splice(2, 0, 'drum');
 5 
 6 // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] 
 7 // removed is [], no elements removed
 8 Remove 1 element from index 3Section
 9 var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
10 var removed = myFish.splice(3, 1);
11 
12 // removed is ["mandarin"]
13 // myFish is ["angel", "clown", "drum", "sturgeon"]
14 Remove 1 element from index 2, and insert "trumpet"Section
15 var myFish = ['angel', 'clown', 'drum', 'sturgeon'];
16 var removed = myFish.splice(2, 1, 'trumpet');
17 
18 // myFish is ["angel", "clown", "trumpet", "sturgeon"]
19 // removed is ["drum"]
20 Remove 2 elements from index 0, and insert "parrot", "anemone" and "blue"Section
21 var myFish = ['angel', 'clown', 'trumpet', 'sturgeon'];
22 var removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');
23 
24 // myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"] 
25 // removed is ["angel", "clown"]
26 Remove 2 elements from index 2Section
27 var myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon'];
28 var removed = myFish.splice(myFish.length - 3, 2);
29 
30 // myFish is ["parrot", "anemone", "sturgeon"] 
31 // removed is ["blue", "trumpet"]
32 Remove 1 element from index -2Section
33 var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
34 var removed = myFish.splice(-2, 1);
35 
36 // myFish is ["angel", "clown", "sturgeon"] 
37 // removed is ["mandarin"]
38 Remove all elements after index 2 (incl.)Section
39 var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
40 var removed = myFish.splice(2);
41 
42 // myFish is ["angel", "clown"] 
43 // removed is ["mandarin", "sturgeon"]
44 Specifications
原文地址:https://www.cnblogs.com/lujunan/p/10315101.html