public static String[] splitCSV(String str,String delim){
  if (str == null || str.isEmpty())   return new String[]{""};
  ArrayList<String> tokens=new ArrayList<String>();
  int from=0, to=0;
  int len=str.length();
  while (from < len) {
    if (str.charAt(from) == CSV_QUOTE_CHAR && str.indexOf(CSV_QUOTE_CHAR,from + 1) > 0) {
      to=str.indexOf(CSV_QUOTE_CHAR,from + 1);
      while (to + 1 < len && str.charAt(to + 1) == CSV_QUOTE_CHAR)       to=str.indexOf(CSV_QUOTE_CHAR,to + 2);
      to+=1;
    }
 else     if (str.regionMatches(from,delim,0,delim.length())) {
      to=from;
    }
 else {
      to=str.indexOf(delim,from + 1);
    }
    to=(to >= 0) ? to : len;
    tokens.add(str.substring(from,to));
    from=to + delim.length();
  }
  if (from == len)   tokens.add("");
  return tokens.toArray(new String[0]);
}

public static String [ ] splitCSV ( String str , String delim ) { if ( str == null || str . isEmpty ( ) ) return new String [ ] { "" } ; ArrayList < String > tokens = new ArrayList < String > ( ) ; int from = 0 , to = 0 ; int len = str . length ( ) ; while ( from < len ) { if ( str . charAt ( from ) == CSV_QUOTE_CHAR && str . indexOf ( CSV_QUOTE_CHAR , from + 1 ) > 0 ) { to = str . indexOf ( CSV_QUOTE_CHAR , from + 1 ) ; while ( to + 1 < len && str . charAt ( to + 1 ) == CSV_QUOTE_CHAR ) to = str . indexOf ( CSV_QUOTE_CHAR , to + 2 ) ; to += 1 ; } else if ( str . regionMatches ( from , delim , 0 , delim . length ( ) ) ) { to = from ; } else { to = str . indexOf ( delim , from + 1 ) ; } to = ( to >= 0 ) ? to : len ; tokens . add ( str . substring ( from , to ) ) ; from = to + delim . length ( ) ; } if ( from == len ) tokens . add ( "" ) ; return tokens . toArray ( new String [ 0 ] ) ; }
splits a string by a specified delimiter into all tokens , including empty while respecting the rules for quotes and escapes defined in rfc4180 . note : use stringescapeutils . unescapecsv ( tmp ) if needed afterwards .
