[英]Remove space after comma in String regex java
I have a string which contain comma separated data:我有一个包含逗号分隔数据的字符串:
1, What is your name ?, Male, Cell no
2, From which country you belong, USA, Greetings
I'm using this regex but it doesn't do the job:我正在使用这个正则表达式,但它不起作用:
str.replace(/\s/g, "")
Hwo can I convert the String to:我可以将字符串转换为:
1,What is your name ?,Male,Cell no
2,From which country you belong,USA,Greetings
As you mention in a comment , you want to trim the values before writing them to CSV.正如您在评论中提到的,您希望在将值写入 CSV 之前对其进行修剪。 This can be done by setting the
withTrim
-option on the CSVFormat
:这可以通过设置来完成
withTrim
在-option CSVFormat
:
CSVFormat csvFormat = CSVFormat.DEFAULT.withTrim();
try (FileWriter writer = new FileWriter("file.txt", false);
CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
csvPrinter.printRecord("1", " What is your name ?", " Male", " Cell no");
csvPrinter.printRecord("2", " From which country you belong", " USA", " Greetings");
}
Content of file.txt
after execution:执行后
file.txt
内容:
1,What is your name ?,Male,Cell no
2,From which country you belong,USA,Greetings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.