用逗号分割句子并删除周围的空格 - JavaScript?
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/6 11:56:17
假设以下是带有逗号和空格 − 的字符串
var sentence = " John , David , Bob , Mike, Carol ";
要用逗号分割句子,请使用 split()。要删除周围的空格,请使用 trim()。
示例
以下是代码 −
var sentences = " John , David , Bob , Mike, Carol ";
console.log("The value=" + sentences);
var result = sentences.split(",").map(function (value) {
return value.trim();
});
console.log("After modifying the value=")
console.log(result);
要运行上述程序,请使用以下命令 −
node fileName.js.
这里,文件名是 demo235.js。
输出
输出如下 −
PS C:\Users\Amit\javascript-code> node demo235.js The value= John , David , Bob , Mike, Carol After modifying the value= [ 'John', 'David', 'Bob', 'Mike', 'Carol' ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

