如何在JavaScript中获取字符串数组的字符串?
我在想一个像"Hello world!"数组一样的字符串['H','e','l','l','o',' ','w','o','r','l','d','!']
var s = "overpopulation";
var ar = [];
ar = s.split();
alert(ar);
Run Code Online (Sandbox Code Playgroud)
我想将string.split一个单词转换为字符数组.
上面的代码似乎不起作用 - 它将"overpopulation"作为Object返回..
如果原始字符串不包含逗号和空格,我如何将其拆分为字符数组?
我有字符串"513".我需要数组["5","1","3"].我的解决方案:
function nextBigger(num){
let numStr = '' + num;
let numArr = [];
for(let i = 0; i < numStr.length; ++i) {
numArr.push(numStr[i]);
}
console.log(numArr);
}
nextBigger(513);
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案很大且多余.我需要更短的解决方案