Java CSV File Parse parseCsvLine(final String line)

Here you can find the source of parseCsvLine(final String line)

Description

parse Csv Line

License

Open Source License

Declaration

public static String[] parseCsvLine(final String line) 

Method Source Code


//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*//from   ww  w. j a v a 2 s.  c o m
 * CsvUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2014 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String[] parseCsvLine(final String line) {
        return parseCsvLine(',', '"', line);
    }

    public static String[] parseCsvLine(final char sep, final char quote, final String line) {
        final List<String> fields = new ArrayList<String>();

        StringBuilder currentField = new StringBuilder();
        boolean inquote = false;
        for (char c : line.toCharArray()) {
            if (!inquote && c == sep) {
                fields.add(currentField.toString());
                currentField = new StringBuilder();
            } else if (c == quote) {
                inquote = !inquote;
            } else {
                currentField.append(c);
            }
        }
        fields.add(currentField.toString());

        return fields.toArray(new String[fields.size()]);
    }
}

Related

  1. parseCSV(String csv)
  2. parseCSV(String csvString)
  3. parseCSV(String data, char... ch)
  4. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  5. parseCSVIntegers(String csv)
  6. parseCSVLine(String CSVLine, char delimChar, char quotChar)
  7. parseCsvRecord(String record, char csvSeparator)
  8. parseCsvString(String toParse)
  9. parseExcelCSVLine(String line)