繁体   English   中英

在String regex java中删除逗号后的空格

[英]Remove space after comma in String regex java

我有一个包含逗号分隔数据的字符串:

1,  What is your name ?,  Male,  Cell no
2,  From which country you belong,  USA,  Greetings

我正在使用这个正则表达式,但它不起作用:

 str.replace(/\s/g, "")

我可以将字符串转换为:

1,What is your name ?,Male,Cell no
2,From which country you belong,USA,Greetings

正如您在评论中提到的,您希望在将值写入 CSV 之前对其进行修剪。 这可以通过设置来完成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");
}

执行后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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM