Java CSV String Split csvToTrimArray(CharSequence charsequence)

Here you can find the source of csvToTrimArray(CharSequence charsequence)

Description

csv To Trim Array

License

Open Source License

Declaration

public static String[] csvToTrimArray(CharSequence charsequence) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of Rinzo/*  ww w.ja va  2  s . co m*/
 * 
 * Author: Claudio Cancinos WWW: https://sourceforge.net/projects/editorxml Copyright (C): 2008, Claudio Cancinos
 * 
 * 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 2 of the License, or (at your option) any
 * later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this program; If not, see
 * <http://www.gnu.org/licenses/>
 ****************************************************************************/

import java.util.ArrayList;

public class Main {
    public static String[] csvToTrimArray(CharSequence charsequence) {
        ArrayList<String> arraylist = new ArrayList<String>();
        StringBuffer stringbuffer = new StringBuffer();
        int i = -1;
        int j = 0;
        for (int k = charsequence.length(); j < k; j++) {
            char c = charsequence.charAt(j);
            if (c == ',') {
                if (stringbuffer.length() > 0) {
                    addTrimmed(stringbuffer, arraylist, i);
                    i = -1;
                }
            } else if (c > ' ' || i >= 0) {
                if (c > ' ')
                    i = stringbuffer.length();
                stringbuffer.append(c);
            }
        }

        if (stringbuffer.length() > 0)
            addTrimmed(stringbuffer, arraylist, i);
        return arraylist.toArray(new String[arraylist.size()]);
    }

    private static void addTrimmed(StringBuffer stringbuffer,
            java.util.List<String> list, int i) {
        int j = i + 1;
        stringbuffer.setLength(j);
        list.add(stringbuffer.toString());
        stringbuffer.setLength(0);
    }
}

Related

  1. csvSplit(String csvLine)
  2. csvToArray(String s)
  3. getContentsFromNumpyCSVString( String numpyString)
  4. getCsvValues(String line)
  5. getValuesFromCSVString(String csvString)
  6. SplitCSV(String csv)