1
temp = line.split(",");
if (i < paymentFieldsMapIndex.size()) {
    paymentFields.put(paymentFieldsMap.get(next).toString(), temp[i]);
}

This code splits up a comma delimited string into substrings and populates a HashMap value using the resulting substrings.

Some substring values look funny in the resulting HashMap, looks like it's due to the presence of commas in the token.

For instance

,"LONDON,UNITED KINGDOM",

in the string appears in the HashMap like

Key = key, Value = "LONDON

I thought that String split(), will not break up substrings containing the delimiter if they are enclosed in double quotes?

I've also tried escaping the embedded comma like

,"LONDON\,UNITED KINGDOM",

but the string in the HashMap looks like

Key = key, Value = "LONDON\

Am I missing something, or is there any way around this problem? Thanks.

Pshemo
  • 113,402
  • 22
  • 170
  • 242
user619804
  • 2,006
  • 11
  • 42
  • 66
  • 1
    see http://opencsv.sourceforge.net/ – Jayan Jul 20 '12 at 16:53
  • possible duplicate of [Java: splitting a comma-separated string but ignoring commas in quotes](http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes) – Rob Hruska Jul 20 '12 at 16:55

1 Answers1

7

"I thought that String split(), will not break up substrings containing the delimiter if they are enclosed in double quotes?"

Your presumption here is incorrect. split() doesn't care about anything but the regular expression you pass it.

If you need to have it split while respecting quotes, there are many ways to accomplish that.

Community
  • 1
  • 1
Rob Hruska
  • 111,282
  • 28
  • 160
  • 186