Java CSV String Split splitCSV(String str)

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

Description

split CSV

License

Open Source License

Declaration

public static String[] splitCSV(String str) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    public static String[] splitCSV(String str) {
        ArrayList<String> split = new ArrayList<String>();
        int i = 0;
        int quotes = 0;
        int index = str.indexOf(",");
        int start = 0;
        int stop = str.length();
        while (index != -1) {
            String temp = str.substring(0, index); //from the start to the comma
            for (int j = 0; j < temp.length(); j++) {
                if (temp.charAt(j) == '\"') {
                    quotes++;/*  w ww. j  a  va  2 s .  c o  m*/
                } //if 
            } //for 
            if ((quotes % 2) == 1) {
                String temp2 = str.substring(0, index + 1);
                int index2 = temp2.indexOf(",");
                String temp3 = str.substring(1, 1 + index + index2);
                split.add(temp3);//adds the string
                str = str.substring(1 + index + index2);
                break;
            } //if 
            else {
                split.add(temp);
            } //else
            str = str.substring(index + 1);
            start = index;
            index = str.indexOf(",");
        } //outer while
        split.add(str);
        String[] splitstring = split.toArray(new String[split.size()]);
        return splitstring;
    }
}

Related

  1. getCsvValues(String line)
  2. getValuesFromCSVString(String csvString)
  3. SplitCSV(String csv)
  4. splitCSV(String inputString)
  5. splitCSV(String str)
  6. splitCSV(String str)
  7. splitCSV(String str, String delim)
  8. SplitCSVString(String str)
  9. tokenizeCsv(String input)