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?

5
  • by writing code that does so. replaceAll comes to mind. Or, write your own algorithm, using the split method Commented Apr 7, 2016 at 8:54
  • You can try splitting using quotes instead of commas. Commented Apr 7, 2016 at 9:16
  • i just need to remove the comma inside double quotes in the csv file. thats it Commented Apr 7, 2016 at 9: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. Commented Apr 7, 2016 at 9:58
  • use split method and spilt using double quotes eg. String[] stringList = strTest.split("\","); Commented May 26, 2022 at 9:30

2 Answers 2

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", "--"]
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Will this work with escaped quotation marks as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.