1

My CSV file look like this

"Gates Nissan of Richmond" , "L2T Media, Llc" , "7000000", "--"

I want to remove comma(,) from this "L2T Media, Llc" in CSV

so that, my output will be "L2T Media Llc"

How can I do this in Java coding?

muzahidbechara
  • 253
  • 1
  • 14
Raghul PR
  • 47
  • 1
  • 2
  • 9
  • by writing code that does so. replaceAll comes to mind. Or, write your own algorithm, using the split method – Stultuske Apr 07 '16 at 08:54
  • You can try splitting using quotes instead of commas. – Amber Beriwal Apr 07 '16 at 09:16
  • i just need to remove the comma inside double quotes in the csv file. thats it – Raghul PR Apr 07 '16 at 09:25
  • 1
    May I ask why you want to remove the comma? - I just ask because it is a completely *well-formed* CSV line to me. And I get the idea that eliminating the comma is just a workaround for not fixing the CSV parsing in the consuming software. – Alexander Apr 07 '16 at 09:58
  • use split method and spilt using double quotes eg. String[] stringList = strTest.split("\","); – NPE May 26 '22 at 09:30

2 Answers2

1

You could try something like this:

String str = "\"Gates Nissan of Richmond\" , \"L2T Media, Llc\" , \"7000000\", \"--\"";
List<String> items = new ArrayList<String>();
for(String item : Arrays.asList(str.substring(1, str.length()-1).split("\"+\\s*,\\s*\"+")))
    items.add("\"" + item.replace(",", "") + "\"");

System.out.println(items);

Output:

["Gates Nissan of Richmond", "L2T Media Llc", "7000000", "--"]
muzahidbechara
  • 253
  • 1
  • 14
0

Based on this answer:

public static void main(String[] args) {
    String s = "\"Gates Nissan of Richmond\" , \"L2T Media, Llc\" , \"7000000\", \"--\"";
    String[] splitted = s.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    for(String item : splitted) {
        item.replaceAll(",", "");
    }
}
Community
  • 1
  • 1
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60