0


vue使用split()将字符串分割数组join()将数组转字符串reverse()将数组反转

1.split() 将字符串切割成数组

const str = 'HelloVue2Vue3'
console.log(str.split())
console.log(str.split(''))
console.log(str.split(' '))
console.log(str.split(' ',2))
console.log(str.split(' ',6))

输出如下
1.split()不传参数默认整个字符串作为数组的一个元素,返回包含原始字符串的数组
2.split(‘’)单引号不传参数默认将字符串拆分成一个个字符数组
如输入参数: const str = 123456789’
拆分后:[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
3.split(’ ')将字符串按空格才分成数组,这个用的比较多的如","将字符串根据逗号切割成数组
4.split(separator, limit)
separator :可选参数。用于拆分的字符串或正则表达式。如果省略输出结果如一所示
limit :可选。限制拆分数量的整数。超出限制如五所示。

一:['HelloVue2Vue3']
二:['H','e','l','l','o',' ','V','u','e','2',' ','V','u','e','3']
三:['Hello','Vue2','Vue3']
四:['Hello','Vue2']
五:['Hello','Vue2','Vue3']

2.reverse() 将数组的字符串进行一个倒序反转。【注】: reverse() 方法只对数组起作用。

const str = 'HelloVue2Vue3'
console.log(str.split(' ').reverse())

输出如下

['Vue3','Vue2','Hello']

3.join() 将组数重新整合成一个字符串

const arr =['Vue3','Vue2','Hello']
console.log(arr.join())
console.log(arr.join('*'))

输出如下
1.join()不传默认按,分割数组
2.join(‘*’)按指定字符将数组分割成字符串

Vue3,Vue2,HelloVue3*Vue2*Hello

本文转载自: https://blog.csdn.net/weixin_43749805/article/details/129621336
版权归原作者 旧言. 所有, 如有侵权,请联系我们删除。

“vue使用split()将字符串分割数组join()将数组转字符串reverse()将数组反转”的评论:

还没有评论