@Test public void testToCsv() { assertEquals("a", CsvUtil.toCsvString("a")); assertEquals("a,b", CsvUtil.toCsvString("a", "b")); assertEquals("a,b,", CsvUtil.toCsvString("a", "b", "")); assertEquals("a,\" b \"", CsvUtil.toCsvString("a", " b ")); assertEquals("a,b,\"jo,e\"", CsvUtil.toCsvString("a", "b", "jo,e")); assertEquals("a,b,\"\"\"some\"\"r\"", CsvUtil.toCsvString("a", "b", "\"some\"r")); assertEquals("1997,Ford,E350,\"Super, luxurious truck\"", CsvUtil.toCsvString("1997", "Ford", "E350", "Super, luxurious truck")); assertEquals("1997,Ford,E350,\"Super \"\"luxurious\"\" truck\"", CsvUtil.toCsvString("1997", "Ford", "E350", "Super \"luxurious\" truck")); assertEquals("1,,2", CsvUtil.toCsvString(Integer.valueOf(1), null, Integer.valueOf(2))); assertEquals("\"a\nb\"", CsvUtil.toCsvString("a\nb")); }
public static String toCsvString(Object...elements) { if (ArrayUtil.isEmpty(elements)) { return null; } StringBuilder line = new StringBuilder(); for (int i = 0, last = elements.length - 1; i <= last; i++) { if (elements[i] == null && i != last) { line.append(FIELD_SEPARATOR); continue; } if (elements[i] == null) { continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx; if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) { ndx = 1; } ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx; if (ndx != -1) { line.append(FIELD_QUOTE); } line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE)); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); }
CsvUtil { public static String toCsvString(Object...elements) { if (ArrayUtil.isEmpty(elements)) { return null; } StringBuilder line = new StringBuilder(); for (int i = 0, last = elements.length - 1; i <= last; i++) { if (elements[i] == null && i != last) { line.append(FIELD_SEPARATOR); continue; } if (elements[i] == null) { continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx; if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) { ndx = 1; } ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx; if (ndx != -1) { line.append(FIELD_QUOTE); } line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE)); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); } }
CsvUtil { public static String toCsvString(Object...elements) { if (ArrayUtil.isEmpty(elements)) { return null; } StringBuilder line = new StringBuilder(); for (int i = 0, last = elements.length - 1; i <= last; i++) { if (elements[i] == null && i != last) { line.append(FIELD_SEPARATOR); continue; } if (elements[i] == null) { continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx; if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) { ndx = 1; } ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx; if (ndx != -1) { line.append(FIELD_QUOTE); } line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE)); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); }  }
CsvUtil { public static String toCsvString(Object...elements) { if (ArrayUtil.isEmpty(elements)) { return null; } StringBuilder line = new StringBuilder(); for (int i = 0, last = elements.length - 1; i <= last; i++) { if (elements[i] == null && i != last) { line.append(FIELD_SEPARATOR); continue; } if (elements[i] == null) { continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx; if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) { ndx = 1; } ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx; if (ndx != -1) { line.append(FIELD_QUOTE); } line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE)); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); }  static String toCsvString(Object...elements); static String toCsvString(List<T> elements); static String[] toStringArray(String line); }
CsvUtil { public static String toCsvString(Object...elements) { if (ArrayUtil.isEmpty(elements)) { return null; } StringBuilder line = new StringBuilder(); for (int i = 0, last = elements.length - 1; i <= last; i++) { if (elements[i] == null && i != last) { line.append(FIELD_SEPARATOR); continue; } if (elements[i] == null) { continue; } String field = elements[i].toString(); int ndx = field.indexOf(FIELD_SEPARATOR); ndx = (ndx == -1) ? field.indexOf(FIELD_QUOTE) : ndx; if (ndx == -1 && (field.startsWith(StringPool.Symbol.SPACE) || field.endsWith(StringPool.Symbol.SPACE))) { ndx = 1; } ndx = (ndx == -1) ? StringUtil.indexOfChars(field, SPECIAL_CHARS) : ndx; if (ndx != -1) { line.append(FIELD_QUOTE); } line.append(StringUtil.replace(field, StringPool.Symbol.DOUBLE_QUOTE, DOUBLE_QUOTE)); if (ndx != -1) { line.append(FIELD_QUOTE); } if (i != last) { line.append(FIELD_SEPARATOR); } } return line.toString(); }  static String toCsvString(Object...elements); static String toCsvString(List<T> elements); static String[] toStringArray(String line);  }