简体   繁体   English

如何从 java 文件中的 csv 文件中的特定字段中删除逗号

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

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.我尝试将escapeCsv StringEscapeUtils放在名称列中,但逗号仍然不是 go。

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());
}

It doesn't look like you're using the result of escapeCsv .看起来您没有使用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.此外,正如评论中所述,这不会删除逗号 - 它会用双引号将字符串括起来,这对于 CSV 中带逗号的字符串是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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