Here you can find the source of splitCSV(String str)
public static String[] splitCSV(String str)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; public class Main { /**//from w ww. j a v a 2 s . c o m * "splitCSV" takes the given string and produces an array containing * substrings that were separated by a comma. However, if the comma is within * two quotation marks along with other characters it is considered part of * the substring element in the array. */ public static String[] splitCSV(String str) { int k = 0, count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '"') count++; } if ((count % 2) != 0) throw new IllegalArgumentException("There must be an even number of quotes."); ArrayList<String> newArrLst = new ArrayList<String>(); for (int i = 0; i < str.length(); i++) { if (i == 0 && str.charAt(0) != '"') { while (k < (str.length()) && !(str.charAt(k) == ',')) k++; newArrLst.add(rmvDoubleQts(str.substring(i, k))); i = k - 1; } // if(i=0, str.charAt(0) != ") else if (str.charAt(i) == '"') { i++; k = i; while (k < (str.length() - 1) && !((str.charAt(k) == '"') && (str.charAt(k + 1) == ','))) k++; newArrLst.add(rmvDoubleQts(str.substring(i, k))); i = k; } // if(") else if (str.charAt(i) == ',') { i++; k = i; if (str.charAt(k) != '"') { while (k < (str.length()) && !(str.charAt(k) == ',')) k++; newArrLst.add(rmvDoubleQts(str.substring(i, k))); i = k - 1; } // if (str.char != ") else i--; } // else if (str.char = ',') } // for (i) return newArrLst.toArray(new String[newArrLst.size()]); } /** * "rmvDoubleQts" is a helper method for the "splitCSV" method. It takes the * given word and replaces any double quotation marks with a single quotation * mark. */ public static String rmvDoubleQts(String word) { String newWord = ""; for (int l = 0; l < word.length(); l++) { if (l < (word.length() - 1) && word.charAt(l) == '"' && word.charAt(l + 1) == '"') l++; newWord += word.charAt(l); } // for(l=0, l<=word.length) return newWord; } }