简体   繁体   中英

How to remove comma in a string while writing into csv file with comma separator in java

I am working on a java project. I have to retain a comma separator from my parsed string "," and still should be able to write into csv file. currently my csv file values are separated with comma (",") in it. because of this, if I have comma in a string its divided into two string on csv

ex:

input string: "Animal","Stand","Earth","owner,jeff"

my current output: (which is incorrect )

在此处输入图像描述

Expected output:

在此处输入图像描述

I have the logic in place for writing things into csv, I just need the string to be manipulated in a such way it should in corporate the ",". how to achieve this?

here is my code:

 private static void Addrow(String Animals, String Posture, String pl.net,String owner) throws IOException { String csv_write = csv_file_to_write; CSVWriter writer = new CSVWriter(new FileWriter(csv_write, true)); String [] record = (Animals+","+Posture+","+pl.net+","+owner).split(","); writer.writeNext(record); writer.close(); }

One way to tackle that would be to split it at \",\" and for the first entry remove the " at the beginning and for the last entry remove the " at the end. You will then be left with the list of strings Animal , Stand , Earth , Owner,Jeff and so on...

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