JS字符串

2 篇文章 0 订阅

1、字符串分割

字符串开始的位置是从0不是1

(1)slice(start, end)

slice(),从start开始,到end结束,不包括end,支持数组分割,支持负数

   let test = 'hello world!'
   console.log(test.length) // 12

   console.log(test.slice(1, 9)) // ello wor
   console.log(test.slice(6)) // world!
   console.log(test.slice(9, 1)) // 
   console.log(test.slice(-2)) // d!
   console.log(test.slice(0, -2)) // hello worl
   console.log(test.slice(-4, -2)) //rl
   console.log(test.slice(-2, 4)) // 

①第一个参数比第二个参数大,结果返回空字符串
②传入参数是负数,slice()会先做运算 test.length + 负数参数。

(2)substr(start, length)

substr(),从start开始,返回length长度字符,支持负数,不支持数组

let test = 'hello world!'

console.log(test.substr(1, 9)) // ello wor
console.log(test.substr(6)) // world!
console.log(test.substr(9, 9)) // ld!
console.log(test.substr(20))  // 
console.log(test.substr(-2)) // d!
console.log(test.substr(-8, 4)) // o wo
console.log(test.substr(-8, 0)) // 
console.log(test.substr(-8, -4)) // 
console.log(test.substr(-20)) // hello world!

①传入参数超过length,返回空字符串。
②传入负数,则从字符串的尾部开始算起始位置,-1指最后一个字符;当传入的第一个参数是负数且它的绝对值超过length,负数转化为0,当传入的第二个参数是负数,等价于0,截取0个字符,返回空字符串。

(3)substring(start, stop)

substring(),不接受负数,从 start 开始,不包括stop,不支持数组

let test = 'hello world!'

console.log(test.substring(1, 9)) // ello wor
console.log(test.substring(6)) // world!
console.log(test.substring(9, 9)) // 
console.log(test.substring(20)) // 
console.log(test.substring(-2)) // hello world!
console.log(test.substring(-8, 4)) // hell
console.log(test.substring(-8, 0)) // 
console.log(test.substring(-8, -4)) // 
console.log(test.substring(-20)) // hello world!

①第二个参数==第一个参数,返回空字符串;
②传入两个参数,不管在第一还是第二位置,都会将小的参数作为第一个参数,较大的作为第二个参数;
③任何一个参数为负数或者NaN的时候,自动将其转换为0;
④任何一个参数大于length,按照length处理。

以上三种都不会对原始的字符串进行修改,而是返回新的子集。

(4)split(separator, length)

字符按照字符串或正则分割,输出一个数组,length表示返回的长度,不支持数组。

//以空格为分隔符输出数组
var str = '123 abc 1 2 3 a b c '
var arr = str.split(' ')
console.log(arr)

在这里插入图片描述

var str = '123 abc 1 2 3 a b c'
var arr = str.split(' ', 4)
//第二个参数表示返回数组的最大长度!注意不是原来字符串的,是新输出的数组的
console.log(arr)

在这里插入图片描述

let str = '1,2,3,5,74,7,8'
 console.log(str.split(','))
 // 输出 ["1", "2", "3", "5", "74", "7", "8"]

(5)join(separator)

将数组合并成字符串,用 separator隔离,不支持字符串

var a = ['I', 'am', 'a', 'girl', '英文名', '是', 'gaby']
var arr = a.join(',')
console.log(arr)

在这里插入图片描述

(6)splice(start, length, …args)

数组操作函数,增删改查,不支持字符串,返回数组,从 start开始,删除的length长度,并按args参数个数添加到 start位置。

①删

第一个参数为第一项位置,第二个参数为要删除几个 0数起

//array.splice(index,num),返回值为删除内容,array为结果值
var arr = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr.splice(0, 4))
console.log(arr)

在这里插入图片描述

②增

第一个参数(插入位置),第二个参数(0),第三个参数(插入的项)

//array.splice(index,0,insertValue),返回值为空数组,array值为最终结果值
var arr = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr.splice(2, 0, 'insert'))
console.log(arr)

在这里插入图片描述

③改

第一个参数(起始位置),第二个参数(删除的项数),第三个参数(插入任意数量的项)

//array.splice(index,num,insertValue),返回值为删除内容,array为结果值
var arr = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr.splice(2, 1, 'delete'))
console.log(arr)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值