一、substring与substr

 

substring

substr

概述

返回字符串两个索引之间(或到字符串末尾)的子串 返回字符串从指定位置开始到指定长度的子串

语法

参数

indexStart:一个 0 到字符串长度之间的整数

indexEnd:可选,一个 0 到字符串长度之间的整数

start:开始提取字符的位置,可为负值

length:可选。提取的字符数

描述

1. indexStart = indexEnd,返回一个空字符串

2. indexStart > indexEnd,则效果是两个参数调换了一样

3. 省略 indexEnd,提取字符一直到字符串末尾

4. 任一参数 < 0 或为 NaN,则被当作 0

5. 任一参数 > strLength(字符串长度),则被当作strLength

1. start < 0,则被看作 strLength + start(从字符串倒数算起

2. start < 0 && abs(start) > strLength,则start = 0

3. start >= strLength,返回一个空字符串

4. 任意参数为 NaN,则被当作 0

5. length <= 0,返回一个空字符串

代码

在线执行

 

1)substring描述

返回字符串两个索引之间(或到字符串末尾)的子串。

var anyString = "Mozilla";//字符串长度为7
// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));//indexStart > indexEnd,则像两个参数对换
console.log(anyString.substring(3,-3));//indexEnd < 0,则indexEnd = 0
console.log(anyString.substring(3,NaN));//indexEnd = NaN,则indexEnd = 0
console.log(anyString.substring(-2,3));//indexStart < 0,则indexStart = 0
console.log(anyString.substring(NaN,3));//indexStart = NaN,则indexStart = 0

// 输出 "lla"
console.log(anyString.substring(4,7));
console.log(anyString.substring(7,4));//indexStart > indexEnd,则像两个参数对换,同上

// 输出 ""
console.log(anyString.substring(4,4));//indexStart = indexEnd

// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(0,10));//indexEnd > anyString.length,indexEnd=anyString.length。
console.log(anyString.substring(0));//省略 indexEnd,提取字符一直到字符串末尾

 

2)substr描述

返回字符串从指定位置开始到指定长度的子串。

var str = "abcdefghij";//长度为10
// 输出bc
console.log(str.substr(1,2));
// 输出hi,start=7
console.log(str.substr(-3,2));//start < 0,则被看作 strLength + start,其中 strLength 为字符串的长度
// 输出hij
console.log(str.substr(-3));//start < 0,省略length
// 输出bcdefghij
console.log(str.substr(1));
// 输出ab start=0
console.log(str.substr(-20,2)); //abs(start) > 字符串的长度,start=0
// 输出""
console.log(str.substr(20,2));  //start >= 字符串的长度,输出空字符串
// 输出""
console.log(str.substr(1,0));
console.log(str.substr(1,-5));//length <= 0,返回一个空字符串
console.log(str.substr(1,NaN));//end非数字, end=0
// 输出a
console.log(str.substr(NaN, 1));//start非数字, start=0

 

二、slice与splice

 

slice

splice

概述

将数组的一部分的浅拷贝, 返回到从开始到结束(结束不包括)选择的新数组对象

原始数组不改变

通过删除现有元素和/或添加新元素来更改数组的内容

原始数组会改变

语法

参数

begin:可选,开始提取的索引(从0开始),可为负值

end:可选,结束提取的索引(包含begin,不包含end),可为负值

start:开始修改的索引(从0开始),可为负值

deleteCount:可选,要移除的数组元素的个数

item1,item2...:可选,要添加进数组的元素,从start位置开始

返回

一个含有提取元素的新数组

由被删除的元素组成的一个数组

如果没有删除元素,则返回空数组。

描述

1. 原始数组没有修改,浅复制了原数组的元素

2. 任何参数为负数,改变计算方式,从数组倒数算起

3. 任何参数为NaN,则被当作 0

4. start 省略,start=0

5. start > end,输出空数组

1. 原始数组被修改,在指定位置可添加任意多个元素

2. start <0 为负数,改变计算方式,从数组倒数算起

3. start 或 deleteCount NaN则被当作 0

4. start 省略,输出空数组

5. deleteCount > 数组长度,则deleteCount=数组长度

代码

在线执行

 

1) slice描述

将数组的一部分的浅拷贝, 返回到从开始到结束(结束不包括)选择的新数组对象,原始数组不改变。

var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
var citrus = fruits.slice(1, 3);//包含1 不包含3
// 输出 ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
console.log(fruits);//原始数组没有修改
// 输出 ['Orange', 'Lemon']
console.log(citrus);//浅复制了原数组的元素

// 输出 ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
console.log(fruits.slice());// start 省略,start=0
// 输出 ['Apple', 'Mango']
console.log(fruits.slice(-2));// start < 0,从数组倒数算起
// 输出 ['Apple']
console.log(fruits.slice(-2, -1));// start < 0 && end < 0,从数组倒数算起,倒数第二个元素到倒数第一个元素
// 输出 ['Orange', 'Lemon', 'Apple']
console.log(fruits.slice(1, -1));
// 输出 []
console.log(fruits.slice(2, 1));// start > end,输出空数组
// 输出 ['Banana']
console.log(fruits.slice(NaN, 1));// start非数字,start=0
// 输出 []
console.log(fruits.slice(1, NaN));// end非数字,end=0

 

2) splice描述

通过删除现有元素和/或添加新元素来更改数组的内容,原始数组会改变。

var myFish = ["angel", "clown", "mandarin", "sturgeon"];
// 输出 []
console.log(myFish.splice(2, 0, "drum"));// 空,没有删除元素
console.log(myFish);//原数组被改变 myFish=["angel", "clown", "drum", "mandarin", "sturgeon"]
// 输出 ["drum"]
console.log(myFish.splice(2, 1));// 有删除元素 myFish=["angel", "clown", "mandarin", "sturgeon"]
myFish.splice(2, 1, "splice", "parrot");
console.log(myFish);//添加两个元素 myFish=["angel", "clown", "splice", "parrot", "sturgeon"]
// 输出 ["parrot", "sturgeon"]
console.log(myFish.splice(-2));// start < 0,从数组倒数算起 myFish=["angel", "clown", "splice"]
// 输出 []
console.log(myFish.splice(-2, -1));// start < 0 && deleteCount < 0,空数组
console.log(myFish.splice(1, -1));//空数组
console.log(myFish.splice(1, NaN));// deleteCount非数字,deleteCount=0
console.log(myFish.splice());// start省略
// 输出 ["angel"]
console.log(myFish.splice(NaN, 1));// start非数字,start=0  myFish=["clown", "splice"]
// 输出 ["clown", "splice"]
console.log(myFish.splice(0, 10));// deleteCount > 数组长度

 

 posted on 2017-02-20 10:14  咖啡机(K.F.J)  阅读(630)  评论(0编辑  收藏  举报