Java CSV File Parse parseCsvRecord(String record, char csvSeparator)

Here you can find the source of parseCsvRecord(String record, char csvSeparator)

Description

CSV record parser.

License

Open Source License

Parameter

Parameter Description
record The CSV record.
csvSeparator The CSV field separator to be used.

Return

A List of Strings representing the fields of each CSV record.

Declaration

private static List<String> parseCsvRecord(String record, char csvSeparator) 

Method Source Code

//package com.java2s;
/*//w  w w . j ava 2s  .  co m
 * net/balusc/util/CsvUtil.java
 * 
 * Copyright (C) 2006 BalusC
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * CSV record parser. Convert a CSV record to a List of Strings representing the fields of the
     * CSV record. The CSV record is expected to be separated by the specified CSV field separator.
     * @param record The CSV record.
     * @param csvSeparator The CSV field separator to be used.
     * @return A List of Strings representing the fields of each CSV record.
     */
    private static List<String> parseCsvRecord(String record, char csvSeparator) {

        // Prepare.
        boolean quoted = false;
        StringBuilder fieldBuilder = new StringBuilder();
        List<String> fields = new ArrayList<String>();

        // Process fields.
        for (int i = 0; i < record.length(); i++) {
            char c = record.charAt(i);
            fieldBuilder.append(c);

            if (c == '"') {
                quoted = !quoted; // Detect nested quotes.
            }

            if ((!quoted && c == csvSeparator) // The separator ..
                    || i + 1 == record.length()) // .. or, the end of record.
            {
                String field = fieldBuilder.toString() // Obtain the field, ..
                        .replaceAll(csvSeparator + "$", "") // .. trim ending separator, ..
                        .replaceAll("^\"|\"$", "") // .. trim surrounding quotes, ..
                        .replace("\"\"", "\""); // .. and un-escape quotes.
                fields.add(field.trim()); // Add field to List.
                fieldBuilder = new StringBuilder(); // Reset.
            }
        }

        return fields;
    }
}

Related

  1. parseCSV(String data, char... ch)
  2. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  3. parseCSVIntegers(String csv)
  4. parseCsvLine(final String line)
  5. parseCSVLine(String CSVLine, char delimChar, char quotChar)
  6. parseCsvString(String toParse)
  7. parseExcelCSVLine(String line)
  8. parseLine(String csvLine)