简体   繁体   中英

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. This can be done by setting the withTrim -option on the 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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