public static List<String> parseCsv(String csv,char separator){
  final char QC='"';
  List<String> list=new ArrayList<>();
  StringBuilder field=new StringBuilder();
  boolean inQuote=false;
  for (int i=0; i < csv.length(); i++) {
    char c=csv.charAt(i);
    if (c == QC) {
      if (i + 1 < csv.length() && csv.charAt(i + 1) == QC) {
        i++;
        field.append(QC);
        continue;
      }
 else {
        inQuote=!inQuote;
        if (inQuote) {
          for (int j=0; j < field.length(); j++) {
            if (field.charAt(j) != QC)             break;
          }
        }
 else {
          if (!inQuote) {
            if (i + 1 == csv.length() || csv.charAt(i + 1) == separator)             ;
 else {
              inQuote=true;
              break;
            }
          }
        }
      }
    }
 else     if (c == separator) {
      if (inQuote) {
        field.append(c);
      }
 else {
        list.add(field.toString());
        field.setLength(0);
      }
    }
 else {
      field.append(c);
    }
  }
  if (inQuote)   throw new IllegalArgumentException("malformed csv string: unbalanced quotes in csv: " + csv);
  if (field.length() != 0) {
    list.add(field.toString());
  }
  return list;
}

public static List < String > parseCsv ( String csv , char separator ) { final char QC = '"' ; List < String > list = new ArrayList < > ( ) ; StringBuilder field = new StringBuilder ( ) ; boolean inQuote = false ; for ( int i = 0 ; i < csv . length ( ) ; i ++ ) { char c = csv . charAt ( i ) ; if ( c == QC ) { if ( i + 1 < csv . length ( ) && csv . charAt ( i + 1 ) == QC ) { i ++ ; field . append ( QC ) ; continue ; } else { inQuote = ! inQuote ; if ( inQuote ) { for ( int j = 0 ; j < field . length ( ) ; j ++ ) { if ( field . charAt ( j ) != QC ) break ; } } else { if ( ! inQuote ) { if ( i + 1 == csv . length ( ) || csv . charAt ( i + 1 ) == separator ) ; else { inQuote = true ; break ; } } } } } else if ( c == separator ) { if ( inQuote ) { field . append ( c ) ; } else { list . add ( field . toString ( ) ) ; field . setLength ( 0 ) ; } } else { field . append ( c ) ; } } if ( inQuote ) throw new IllegalArgumentException ( "malformed csv string: unbalanced quotes in csv: " + csv ) ; if ( field . length ( ) != 0 ) { list . add ( field . toString ( ) ) ; } return list ; }
parse a csv string into its fields using the specified field separator .
