【问题标题】:how to separate a string by comma in javascript? [duplicate]如何在javascript中用逗号分隔字符串? [复制]
【发布时间】:2021-06-20 21:07:48
【问题描述】:

如何在javascript中打印如下?

如果我的输入是“智能” 那么我的输出可能是“s,m,a,r,t”

我已尝试使用此逻辑,但我得到的输出为 s,m,a,r,t,

let a = " ";
str = userInput[0];
console.log(str);
for (let i = 0; i < str.length; i++) {
  a = a + str[i] + ",";
}
console.log(a.trim());

【问题讨论】:

标签: javascript


【解决方案1】:

js中可以使用split方法

var str = "smart";
var res = str.split("").join();
console.log(res)
   //if you want it to remain an array don't do the join

【讨论】:

    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多