在JavaScript中,可以使用split()和Array.from()方法将字符串转换为字符串数组。
方法1:使用split()方法
split()方法用于将给定字符串拆分为字符串数组,该方法是使用参数中提供的指定分隔符将其分隔为子字符串。
语法:str.split(separator, limit)
举个例子:HTML>
Welcome to here!
var str =document.getElementById("str").innerHTML;
document.getElementById("arr").innerHTML = str.split("");
结果:Welcome to here!
W,e,l,c,o,m,e, ,t,o, ,h,e,r,e,!
方法2:使用Array.from()方法
Array.from()方法是javascript中的一个内置函数,它从给定的数组创建一个新的数组实例。对于字符串,字符串的每个字母表都会转换为新数组实例的元素;对于整数值,新数组实例simple将获取给定数组的元素。
语法:Array.from(str)
举个例子:HTML>
Welcome to here!
var str = document.getElementById("str").innerHTML;
document.getElementById("arr").innerHTML = Array.from(str);
输出结果:Welcome to here!
W,e,l,c,o,m,e, ,t,o, ,h,e,r,e,!