您的位置:首页 > Web前端 > JavaScript

JavaScript中几个相似方法对比

2017-04-17 11:33 225 查看

一、substring与substr

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

参数indexStart:一个 0 到字符串长度之间的整数indexEnd:可选,一个 0 到字符串长度之间的整数start:开始提取字符的位置,可为负值length:可选。提取的字符数
描述1. indexStart = indexEnd,返回一个空字符串2. indexStart > indexEnd,则效果是两个参数调换了一样3. 省略 indexEnd,提取字符一直到字符串末尾4. 任一参数 < 0 或为 NaN,则被当作 05. 任一参数 > strLength(字符串长度),则被当作strLength1. start < 0,则被看作 strLength + start(从字符串倒数算起)2. start < 0 && abs(start) > strLength,则start = 03. start >= strLength,返回一个空字符串4. 任意参数为 NaN,则被当作 05. 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 = 0console.log(anyString.substring(3,NaN));//indexEnd = NaN,则indexEnd = 0console.log(anyString.substring(-2,3));//indexStart < 0,则indexStart = 0console.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// 输出bcconsole.log(str.substr(1,2));// 输出hi,start=7console.log(str.substr(-3,2));//start < 0,则被看作 strLength + start,其中 strLength 为字符串的长度// 输出hijconsole.log(str.substr(-3));//start < 0,省略length// 输出bcdefghijconsole.log(str.substr(1));// 输出ab start=0console.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// 输出aconsole.log(str.substr(NaN, 1));//start非数字, start=0

二、slice与splice

slicesplice
概述将数组的一部分的浅拷贝, 返回到从开始到结束(结束不包括)选择的新数组对象原始数组不改变通过删除现有元素和/或添加新元素来更改数组的内容原始数组会改变
语法

参数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,则被当作 04. 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=0console.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 > 数组长度

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串 start 倒数