JavaScript substr,substring,slice,splice

来源:互联网 发布:python 依赖注入 编辑:程序博客网 时间:2024/05/16 14:17

str.substr(startIndex,length)

  • startIndex开始取length个字符
  • startIndex为负值,相当于str.length+startIndex
  • startIndex为负值且Math.abs(startIndex)>str.length,则默认将startIndex设为0
  • 省略length则返回从startIndex到字符串末尾
  • length0或者负值则返回空字符

str.substring(startIndex, endIndex)

  • 包括startIndex不包括endIndex
  • 若有参数为负值或者NaN,则默认转换为0
  • startIndex小于endIndex,则默认将他俩调换位置。
  • 若省略endIndex则默认endIndex=str.length

str.slice(startIndex,endIndex) |[].slice(startIndex,endIndex)

  • 浅拷贝
  • startIndex|endIndex为负值,相当于str.length+startIndex|endIndex
  • 省略endIndex则取到字符串末尾
  • 没有参数时即取整个字符串

[].splice(startIndex,deleCount,item1,item2,…)

  • startIndex为负值,相当于str.length+startIndex
  • 省略startIndex默认为0,即splice中只有个参数num时相当与splice(0,num)
  • deleCount<0,默认为0,返回空数组,原数组不变
阅读全文
0 0