Java CSV File Parse parseCsvString(String toParse)

Here you can find the source of parseCsvString(String toParse)

Description

Parse comma separated string into list of tokens.

License

Open Source License

Parameter

Parameter Description
toParse String to parse

Return

List of tokens if at least one token exists, null otherwise.

Declaration

public static List<String> parseCsvString(String toParse) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;

import java.util.List;

public class Main {
    /**// w  ww . j  a  va  2 s .com
     * Parse comma separated string into list of tokens. Tokens are trimmed, empty tokens are not in result.
     * 
     * @param toParse String to parse
     * @return List of tokens if at least one token exists, null otherwise.
     */
    public static List<String> parseCsvString(String toParse) {
        if (toParse == null || toParse.length() == 0) {
            return null;
        }
        String[] t = toParse.split(",");
        if (t.length == 0) {
            return null;
        }
        List<String> ret = new ArrayList<String>();
        for (String s : t) {
            if (s != null) {
                s = s.trim();
                if (s.length() > 0) {
                    ret.add(s);
                }
            }
        }
        if (ret.isEmpty())
            return null;
        else
            return ret;
    }

    /**
     * Check if String value is null or empty.
     * 
     * @param src value
     * @return <code>true</code> if value is null or empty
     */
    public static boolean isEmpty(String src) {
        return (src == null || src.length() == 0 || src.trim().length() == 0);
    }
}

Related

  1. parseCsvFile(String filename, String csvSplitBy, boolean skipHeader)
  2. parseCSVIntegers(String csv)
  3. parseCsvLine(final String line)
  4. parseCSVLine(String CSVLine, char delimChar, char quotChar)
  5. parseCsvRecord(String record, char csvSeparator)
  6. parseExcelCSVLine(String line)
  7. parseLine(String csvLine)