Raghul PR
Raghul PR

Reputation: 47

how to remove commas between the String in the csv file using java

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?

Upvotes: 1

Views: 2820

Answers (2)

muzahidbechara
muzahidbechara

Reputation: 253

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", "--"]

Upvotes: 1

Adriaan Koster
Adriaan Koster

Reputation: 16209

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

Upvotes: 0

Related Questions