array splice split || string split slice 傻傻分不清楚=>终于弄清楚了
Array
1. arr.slice([begin[, end]]) : 方法返回一个新的数组对象,这一对象是一个由 begin
和 end
决定的原数组的浅拷贝(包括 begin
,不包括end
)。原始数组不会被改变。
begin=>开始索引
end=>结束索引
1 var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; 2 var removed = myFish.splice(2, 0, 'drum', 'guitar'); 3 4 // 运算后的 myFish: ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"] 5 // 被删除的元素: [], 没有元素被删除
2. array.splice(start[, deleteCount[, item1[, item2[, ...]]]]) 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组
start=>开始索引
deletecount=>删除的个数
var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
var removed = myFish.splice(3, 1);
// 运算后的 myFish: ["angel", "clown", "drum", "sturgeon"]
// 被删除的元素: ["mandarin"]
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
var removed = myFish.splice(2, 0, 'drum', 'guitar');
// 运算后的 myFish: ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// 被删除的元素: [], 没有元素被删除
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
var removed = myFish.splice(2);
// 运算后的 myFish: ["angel", "clown"]
// 被删除的元素: ["mandarin", "sturgeon"]
String
String.slice()
方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
slice()
提取的新字符串包括beginIndex
但不包括 endIndex
var str1 = 'The morning is upon us.', // str1 的长度 length 是 23。 str2 = str1.slice(1, 8), str3 = str1.slice(4, -2), str4 = str1.slice(12), str5 = str1.slice(30); console.log(str2); // 输出:he morn console.log(str3); // 输出:morning is upon u console.log(str4); // 输出:is upon us. console.log(str5); // 输出:""
String.split([separator[, limit]])
var myString = "Hello World. How are you doing?"; var splits = myString.split(" ", 3); console.log(splits); 上例输出: ["Hello", "World.", "How"]
var str = 'The quick brown fox jumps over the lazy dog.';
var words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
var chars = str.split('');
console.log(chars[8]);
// expected output: "k"
var strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]
str.substring(indexStart[, indexEnd])方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集
提取从 indexStart 到 indexEnd(不包括)之间的字符 var anyString = "Mozilla"; // 输出 "Moz" console.log(anyString.substring(0,3)); console.log(anyString.substring(3,0)); console.log(anyString.substring(3,-3)); console.log(anyString.substring(3,NaN)); console.log(anyString.substring(-2,3)); console.log(anyString.substring(NaN,3)); // 输出 "lla" console.log(anyString.substring(4,7)); console.log(anyString.substring(7,4)); // 输出 "" console.log(anyString.substring(4,4)); // 输出 "Mozill" console.log(anyString.substring(0,6)); // 输出 "Mozilla" console.log(anyString.substring(0,7)); console.log(anyString.substring(0,10));