Divakar R
Divakar R

Reputation: 845

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 )

enter image description here

Expected output :

enter image description here

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 planet,String owner) throws IOException {
        String csv_write = csv_file_to_write;
        CSVWriter writer = new CSVWriter(new FileWriter(csv_write , true));

            String [] record = (Animals+","+Posture+","+planet+","+owner).split(",");

        writer.writeNext(record);

        writer.close();

    }

Upvotes: 0

Views: 789

Answers (1)

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...

Upvotes: 1

Related Questions