字符串转换成数组的几种情况

1、字符串转换成数组
(1)split()方法,方法内什么参数都不传时,该字符串整体为一个数组
例如:

var a='abdcjw'
console.log(a.split())
//结果为:
["abdcjw"]


var b='asd,asd';
console.log(b.split())
//结果为
 ["asd,asd"]

(2)有参数,且参数为 ‘’ 时,把每个字符转成数组的每一个元素

var a='abdcjw'
console.log(a.split(''))
//结果为:
 ["a", "b", "d", "c", "j", "w"]

var b='asd,asd';
console.log(b.split())
//结果为
["a", "s", "d", ",", "a", "s", "d"]

(3)有参数切正常普通的字符参数
例如:

情况1:两个切割参数挨着
var a='sddffss'
console.log(a.split('f'))
//结果为
["sdd", "", "ss"]

情况2:切割参数为特殊字符
var b ='anshd,ashj'
console.log(b.split(','))
//结果为
["anshd", "ashj"]

情况3:切割参数为单个字母
var c ='ahkjasd'
console.log(c.split('k'))
//结果为
["ah", "jasd"]

情况5:参数在边缘
var d ='ahkjasd'
console.log(d.split('d'))
//结果为
["ahkjas", ""]

你可能感兴趣的:(前端,js)