Java CSV String Split splitCSV(String inputString)

Here you can find the source of splitCSV(String inputString)

Description

split CSV

License

Open Source License

Declaration

public static String[] splitCSV(String inputString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    public static String[] splitCSV(String inputString) {
        ArrayList<String> stringList = new ArrayList<String>();
        String tempString;//  w  ww  .j a v  a  2s. c om
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < inputString.length(); i++) {
            if (inputString.charAt(i) == '"') {
                i++;
                while (i < inputString.length()) {
                    if (inputString.charAt(i) == '"' && inputString.charAt(i + 1) == '"') //when there are two double quotation mark next to each other
                    {
                        sb.append('"');
                        i = i + 2;
                    }
                    if (inputString.charAt(i) == '"') {
                        break;
                    } else {
                        sb.append(inputString.charAt(i));
                        i++;
                    }
                }
                i++; // skip over the double quotation mark
            }

            if (inputString.charAt(i) != ',') {
                sb.append(inputString.charAt(i));
            } // if
            else {
                tempString = sb.toString();
                stringList.add(tempString);
                sb.setLength(0);
            } // else
        } // for

        // After reaching the last character of the string, we need to do the
        // following
        tempString = sb.toString();
        stringList.add(tempString);
        sb.setLength(0);

        String[] stockArr = new String[stringList.size()];
        stockArr = stringList.toArray(stockArr);
        return stockArr;
    }
}

Related

  1. csvToTrimArray(CharSequence charsequence)
  2. getContentsFromNumpyCSVString( String numpyString)
  3. getCsvValues(String line)
  4. getValuesFromCSVString(String csvString)
  5. SplitCSV(String csv)
  6. splitCSV(String str)
  7. splitCSV(String str)
  8. splitCSV(String str)
  9. splitCSV(String str, String delim)