Java CSV String Split csvToArray(String s)

Here you can find the source of csvToArray(String s)

Description

csv To Array

License

Open Source License

Declaration

public static String[] csvToArray(String s) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of Rinzo// w  w  w  . j  a v  a 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[] csvToArray(String s) {
        ArrayList<String> arraylist = new ArrayList<String>();
        boolean flag = false;
        boolean flag1 = true;
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c <= ' ')
                flag = true;
            else if (c == ',') {
                arraylist.add(stringbuffer.toString());
                stringbuffer.setLength(0);
                flag1 = true;
                flag = false;
            } else {
                if (!flag1 && flag)
                    stringbuffer.append(' ');
                flag1 = flag = false;
                stringbuffer.append(c);
            }
        }

        arraylist.add(stringbuffer.toString());
        return arraylist.toArray(new String[arraylist.size()]);
    }

    public static String[] csvToArray(CharSequence charsequence) {
        ArrayList<String> arraylist = new ArrayList<String>();
        StringBuffer stringbuffer = new StringBuffer();
        int i = 0;
        for (int j = charsequence.length(); i < j; i++) {
            char c = charsequence.charAt(i);
            if (c == ',') {
                if (stringbuffer.length() > 0) {
                    arraylist.add(stringbuffer.toString());
                    stringbuffer.setLength(0);
                }
            } else {
                stringbuffer.append(c);
            }
        }

        if (stringbuffer.length() > 0)
            arraylist.add(stringbuffer.toString());
        return arraylist.toArray(new String[arraylist.size()]);
    }
}

Related

  1. csvSplit(String csvLine)
  2. csvToTrimArray(CharSequence charsequence)
  3. getContentsFromNumpyCSVString( String numpyString)
  4. getCsvValues(String line)
  5. getValuesFromCSVString(String csvString)