CsvUtil { public static String toCsvString(Object... elements) { StringBuilder line = new StringBuilder(); int last = elements.length - 1; for (int i = 0; i < elements.length; i++) { if (elements[i] == null) { if (i != last) { line.append(FIELD_SEPARATOR); } continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); if (ndx == -1) { ndx = field.indexOf(FIELD_QUOTE); } if (ndx == -1 && (field.startsWith(SPACE) || field.endsWith(SPACE))) { ndx = 1; } if (ndx == -1) { ndx = StringUtils.indexOf(field, SPECIAL_CHARS); } if (ndx != -1) { line.append(FIELD_QUOTE); } field = StringUtils.replace(field, QUOTE, DOUBLE_QUOTE); line.append(field); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); }  static String toCsvString(Object... elements); static String[] fromCsvString(String line);  }
@Test public void toCsvString() { assertThat(CsvUtil.toCsvString(1, 2)).isEqualTo("1,2"); assertThat(CsvUtil.toCsvString(1, 2, 3, 4)).isEqualTo("1,2,3,4"); assertThat(CsvUtil.toCsvString(1, "2")).isEqualTo("1,2"); assertThat(CsvUtil.toCsvString(1, "A BC")).isEqualTo("1,A BC"); assertThat(CsvUtil.toCsvString(1, "A,BC")).isEqualTo("1,\"A,BC\""); assertThat(CsvUtil.toCsvString(1, "A\"BC")).isEqualTo("1,\"A\"\"BC\""); assertThat(CsvUtil.toCsvString(1, "A,\"a\"BC")).isEqualTo("1,\"A,\"\"a\"\"BC\""); }