1、使用 split() 方法:
let str = "Hello, World!";
let arr = str.split(" "); // ["Hello,", "World!"]
2、使用扩展运算符(…)和 split() 方法:
let str = "Hello, World!";
let arr = [...str.split(" ")]; // ["Hello,", "World!"]
3、使用 Array.from() 方法:
let str = "Hello, World!";
let arr = Array.from(str, function(char) { return char; });
// ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]
4、数组转字符串
const array = ['apple', 'banana', 'orange'];
const string = array.join(', '); // 使用逗号和空格作为分隔符
console.log(string); // 输出 "apple, banana, orange"
本文介绍了JavaScript中字符串处理的四个常见方法:使用split()分割字符串,扩展运算符处理split结果,Array.from()转换字符数组,以及数组join()将数组转为字符串。
3757

被折叠的 条评论
为什么被折叠?



