Java CSV File Parse parseCSV(String csv)

Here you can find the source of parseCSV(String csv)

Description

parse CSV

License

Apache License

Declaration

public static String[] parseCSV(String csv) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String[] parseCSV(String csv) {
        List<String> vals = new ArrayList<String>();
        boolean quoted = false;

        StringBuffer val = new StringBuffer();

        for (int i = 0; i < csv.length(); i++) {
            char c = csv.charAt(i);

            if (c == '\"') {
                if (quoted) {
                    quoted = false;/*  w w w . java2s .c o m*/
                } else {
                    quoted = true;
                }

                if (i > 0 && csv.charAt(i - 1) == '\"') {
                    val.append(c);
                }
            } else if (quoted) {
                val.append(c);
            } else if (c == ',') {
                vals.add(val.toString().trim());
                val.setLength(0);
            } else {
                val.append(c);
            }
        }

        vals.add(val.toString().trim());

        return (String[]) vals.toArray(new String[] {});
    }
}

Related

  1. csvSplitLine(String line, char fieldSeparator)
  2. parse(final String csvString)
  3. parseCsv(InputStream csvInput)
  4. parseCsv(InputStream fileStream)
  5. parseCSV(String csv)
  6. parseCSV(String csvString)
  7. parseCSV(String data, char... ch)
  8. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  9. parseCSVIntegers(String csv)