CsvUtil { public static String[] fromCsvString(String line) { List<String> row = new ArrayList<String>(); boolean inQuotedField = false; int fieldStart = 0; final int len = line.length(); for (int i = 0; i < len; i++) { char c = line.charAt(i); if (c == FIELD_SEPARATOR) { if (!inQuotedField) { addField(row, line, fieldStart, i, inQuotedField); fieldStart = i + 1; } } else if (c == FIELD_QUOTE) { if (inQuotedField) { if (i + 1 == len || line.charAt(i + 1) == FIELD_SEPARATOR) { addField(row, line, fieldStart, i, inQuotedField); fieldStart = i + 2; i++; inQuotedField = false; } } else if (fieldStart == i) { inQuotedField = true; fieldStart++; } } } if (len > 0 && fieldStart <= len) { addField(row, line, fieldStart, len, inQuotedField); } return row.toArray(new String[row.size()]); }  static String toCsvString(Object... elements); static String[] fromCsvString(String line);  }
@Test public void fromCsvString() { assertThat(CsvUtil.fromCsvString("1,2")).hasSize(2).contains("1").contains("2"); assertThat(CsvUtil.fromCsvString("1,A BC")).hasSize(2).contains("1").contains("A BC"); assertThat(CsvUtil.fromCsvString("1,\"A,BC\"")).hasSize(2).contains("1").contains("A,BC"); assertThat(CsvUtil.fromCsvString("1,\"A,\"\"a\"\"BC\"")).hasSize(2).contains("1").contains("A,\"a\"BC"); assertThat(CsvUtil.fromCsvString("1,\"A,\"a\"\"BC\"")).hasSize(2).contains("1").contains("A,\"a\"BC"); assertThat(CsvUtil.fromCsvString("1,ABC\"")).hasSize(2).contains("1").contains("ABC\""); }