Java CSV String Create toCsvLine(final String[] parts)

Here you can find the source of toCsvLine(final String[] parts)

Description

Turns a string array into a CSV line.

License

Open Source License

Parameter

Parameter Description
parts the columns of the CSV line

Return

the combined CSV string without trailing newline

Declaration

public static String toCsvLine(final String[] parts) 

Method Source Code

//package com.java2s;
/*/* ww w . j a  v a  2 s  . c om*/
 * This file is part of the "STARDUST" project.
 *
 * (c) Fabian Keller <hello@fabian-keller.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

public class Main {
    /**
     * used CSV delimiter
     */
    public static final String CSV_DELIMITER = ";";
    /**
     * used quote character to quote fields
     */
    public static final String CSV_QUOTE = "\"";

    /**
     * Turns a string array into a CSV line.
     *
     * @param parts the columns of the CSV line
     * @return the combined CSV string without trailing newline
     */
    public static String toCsvLine(final String[] parts) {
        final StringBuffer line = new StringBuffer("");
        for (final String part : parts) {
            if (line.length() > 0) {
                line.append(CSV_DELIMITER);
            }
            line.append(part.replaceAll(CSV_QUOTE, CSV_QUOTE + CSV_QUOTE));
        }
        return line.toString();
    }
}

Related

  1. toCSV(int[] a)
  2. toCsv(Object[] arr, String format)
  3. toCsv(String value)
  4. toCSV3_2(double[][][] array, int index1, int index3)
  5. toCSVBuffer(byte barr[])
  6. toCSVString(double[] d)
  7. toCsvString(Object object)
  8. toCSVString(Object[] pStringArray)
  9. toCSVString(String s)