如何从字符串中删除所有逗号并添加空格? [英] How to remove all commas from a string and add white space?
本文介绍了如何从字符串中删除所有逗号并添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于这两个字符串:
var a = "November 5, 1916";
var b = "October 5–10, 1592";
我在做:
b.replace(' ', '').split(' ');
a.replace(' ', '').split(' ');
但我仍然得到一个逗号,没有空格。我需要能够从这些字符串中删除所有逗号并保留空格以便:
But I still get a comma and no white space. I need to be able to remove ALL commas from those strings and keep the white space in order to have:
var resultA = "November 5 1916";
var resultB = "October 5–10 1592";
更新
之后我确实需要 split()
,因为我需要数组中的每个字符串。
I do need the split()
afterwards as I need each string in an array.
推荐答案
不需要split()方法,因为您没有尝试将字符串转换为数组。
The split() method is not needed since you are not trying to turn the string into an array.
只需直接删除所有逗号:
Just to remove all commas straighforwardly:
a.replace(',', '');
b.replace(',', '');
这篇关于如何从字符串中删除所有逗号并添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文