一些容易记混的方法

1.substr和substring

    var sh = 'nishuonixiangyaota';下标是从0开始的
       console.log(sh.substr()) //如果不写值返回整个字符
    console.log(sh.substr(2, 4));//shuo    (start,length)
    console.log(sh.substring(2, 6));//shuo     (start,end)//不包含结束的那个

2.slice和splice

slice() 方法可从已有的数组中返回选定的元素。

splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。

注释:该方法会改变原始数组。

    var arrd = [2, 3, 4, 5, 6];
    console.log(arrd.slice())//[2,3,4,5,6]
    console.log(arrd.slice(0, 2)) //[2,3] 
    console.log(arrd.splice(0, 1), arrd);//[3,4,5,6]  //把第0个删除了
    console.log(arrd.splice(1, 0, 'xinzeng'), arrd); //arrd :[3, "xinzeng", 4, 5, 6]
    console.log(arrd.splice(1, 1, '替换'), arrd); //arrd : [3, "替换", 4, 5, 6]
原文地址:https://www.cnblogs.com/SunShineM/p/8566600.html