用逗号分隔句子并去掉前后空白 - JavaScript?
假设以下内容是带有逗号和空白的字符串 −
var sentences = " 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' ]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP