Java CSV File Parse parse(final String csvString)

Here you can find the source of parse(final String csvString)

Description

Splits the csv string.

License

Open Source License

Parameter

Parameter Description
string a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static List<String> parse(final String csvString) throws Exception 

Method Source Code

//package com.java2s;
/*//w w w . j a  v  a2s .c om
 * Copyright (c) 2015 Christopher Ritchie
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Splits the string based on the given separator.
     * @param string
     * @param separator
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String string, final String separator, boolean trim) throws Exception {
        String left = string;
        List<String> tokens = new ArrayList<String>();
        while (left.indexOf(separator) >= 0) {
            int index = left.indexOf(separator);
            if (trim) {
                tokens.add(left.substring(0, index).trim());
            } else {
                tokens.add(left.substring(0, index));
            }
            left = left.substring(index + separator.length());
        }
        tokens.add(left.substring(0));
        return tokens;
    }

    /**
     * Splits the csv string.
     * @param string
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String csvString, boolean trim) throws Exception {
        return parse(csvString, ",", trim);
    }

    /**
     * Splits the csv string.
     * @param string
     * @return
     * @throws Exception
     */
    public static List<String> parse(final String csvString) throws Exception {
        return parse(csvString, ",", true);
    }
}

Related

  1. csvLineParse(String line, char delimiter)
  2. csvSplitLine(String line, char fieldSeparator)
  3. parseCsv(InputStream csvInput)
  4. parseCsv(InputStream fileStream)
  5. parseCSV(String csv)
  6. parseCSV(String csv)