Skip to content
Advertisement

Java: splitting a comma-separated string but ignoring commas in quotes

I have a string vaguely like this:

JavaScript

that I want to split by commas — but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit: I guess I meant libraries that are already part of the JDK or already part of a commonly-used libraries like Apache Commons.)

the above string should split into:

JavaScript

note: this is NOT a CSV file, it’s a single string contained in a file with a larger overall structure

Advertisement

Answer

Try:

JavaScript

Output:

JavaScript

In other words: split on the comma only if that comma has zero, or an even number of quotes ahead of it.

Or, a bit friendlier for the eyes:

JavaScript

which produces the same as the first example.

EDIT

As mentioned by @MikeFHay in the comments:

I prefer using Guava’s Splitter, as it has saner defaults (see discussion above about empty matches being trimmed by String#split(), so I did:

JavaScript
Advertisement