How to remove a comma from a particular field in csv file in java

545 views Asked by At

My code below is printing the columns with the data. I tried putting escapeCsv method of StringEscapeUtils but comma does not still go from the name column.

builder.append("Code,CodeName,CodeDepartment");
builder.append(System.lineSeparator());
Set<Entry<String, CodeDetails>> entrySet = sorted.entrySet();
for(Entry<String, CodeDetails> entry : entrySet)
{
    builder.append(entry.getKey());
    builder.append(",");
    builder.append(entry.getValue().getCodeName());
    StringEscapeUtils.escapeCsv(entry.getValue().getCodeName());
    builder.append(",");
    builder.append(entry.getValue().getCodeDepartment());
}
1

There are 1 answers

2
Logan Pickup On BEST ANSWER

It doesn't look like you're using the result of escapeCsv. You should try something like:

builder.append(StringEscapeUtils.escapeCsv(entry.getValue().getCodeName()));

Also, as noted in the comments, this won't remove the comma - it will surround the string with double quotes, which is correct for strings with commas in CSV.