Take the 2-minute tour ×

I see that there are several similar questions, but I have not found any of the answers satisfactory. I have a comma delimited file where each line looks something like this:

4477,52544,,,P,S,    ,,SUSAN JONES,9534 Black Bear Dr,,"CITY, NV 89506",9534 BLACK BEAR DR,,CITY,NV,89506,2008,,,,  ,     ,    , ,,1

The problem that comes into play is when a token escapes a comma with quotes "CITY, NV 89506"

I need a result where the escaped tokens are handled and every token is included, even empty ones

Would appreciate any help!

share|improve this question
whathaveyoutried.com ? ;) –  Morten Jensen Oct 4 '12 at 23:22
I'd be tempted to do the parsing myself. It doesn't seem too difficult. –  Ted Hopp Oct 4 '12 at 23:24
@TedHopp show me the code ;) –  MadProgrammer Oct 4 '12 at 23:39
Splitting on this regex, which i found on another question, gets me pretty close regex = ",(?=([^"]*"[^"]*")*[^"]*$)". The problem is i end up with quotes in the results. I can't figure out how to remove the quotes. –  springcorn Oct 5 '12 at 0:24
This question has been asked many times, actually. The keywords are quite variant though. See stackoverflow.com/questions/6432408/… and stackoverflow.com/questions/6428053/… for example. –  acheong87 Oct 5 '12 at 3:03
show 2 more comments

2 Answers

Here is one way to answer your question using delivered java.lang.String methods. I believe it does what you need.

private final char QUOTE = '"';
private final char COMMA = ',';
private final char SUB = 0x001A; // or whatever character you know will NEVER
    // appear in the input String

public void readLine(String line) {
    System.out.println("original: " + line);

    // Replace commas inside quoted text with substitute character
    boolean quote = false;
        for (int index = 0; index < line.length(); index++) {
        char ch = line.charAt(index);
        if (ch == QUOTE) {
            quote = !quote;
        } else if (ch == COMMA && quote) {
            line = replaceChar(line, index, SUB);
            System.out.println("replaced: " + line);

    // Strip out all quotation marks
        if (line.charAt(index) == QUOTE) {
            line = removeChar(line, index);
            System.out.println("stripped: " + line);

    // Parse input into tokens
    String[] tokens = line.split(",");
    // restore commas in place of SUB characters
        tokens[i] = tokens[i].replace(SUB, COMMA);

    // Display final results
    System.out.println("Final Parsed Tokens: ");
    for (String token : tokens) {
        System.out.println("[" + token + "]");

private String replaceChar(String input, int position, char replacement) {
    String begin = input.substring(0, position);
    String end = input.substring(position + 1, input.length());
    return begin + replacement + end;

private String removeChar(String input, int position) {
    String begin = input.substring(0, position);
    return begin + end;

Hope this helps

share|improve this answer
add comment

Consider a proper CSV parser such as opencsv. It will be highly tested (unlike a new, home-grown solution) and handle edge-conditions such as the one you describe (and lots you haven't thought about).

In the download, there is an examples folder which contains "addresses.csv" with this line:

Jim Sample,"3 Sample Street, Sampleville, Australia. 2615",jim@sample.com

In the same directory, the file AddressExample.java parses this file, and is highly relevant to your question.

share|improve this answer
add comment

Your Answer


