CsvUtil { public static String[] toStringArray(String line) { List<String> row = CollectionUtil.createArrayList(); 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; } continue; } 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 toCsvString(List<T> elements); static String[] toStringArray(String line);  }
@Test public void testFromCsv() { assertStringArray(CsvUtil.toStringArray("a"), "a"); assertStringArray(CsvUtil.toStringArray("a,b"), "a", "b"); assertStringArray(CsvUtil.toStringArray("a, b "), "a", " b "); assertStringArray(CsvUtil.toStringArray("a,\" b \""), "a", " b "); assertStringArray(CsvUtil.toStringArray("a,b,"), "a", "b", ""); assertStringArray(CsvUtil.toStringArray("a,b,\"jo,e\""), "a", "b", "jo,e"); assertStringArray(CsvUtil.toStringArray("a,b,\"\"\"some\"\"r\""), "a", "b", "\"some\"r"); assertStringArray(CsvUtil.toStringArray("1997,Ford,E350,\"Super, luxurious truck\""), "1997", "Ford", "E350", "Super, luxurious truck"); assertStringArray(CsvUtil.toStringArray("1997,Ford,E350,\"Super \"\"luxurious\"\" truck\""), "1997", "Ford", "E350", "Super \"luxurious\" truck"); assertStringArray(CsvUtil.toStringArray("\"a\nb\""), "a\nb"); assertStringArray(CsvUtil.toStringArray("a,,b"), "a", "", "b"); }