Java CSV String Create toCsv(String value)

Here you can find the source of toCsv(String value)

Description

Formats a string for CSV escaping it as a double quoted CSV string if necessary

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration

public static String toCsv(String value) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w  w  w.j a v  a  2s  .c  om
     * Formats a string for CSV escaping it as a double quoted CSV string if necessary
     * @param value
     */
    public static String toCsv(String value) {
        if (value.contains(",")) {
            return "\"" + escapeQuotesForCsv(value) + "\"";
        } else {
            return value;
        }
    }

    /**
     * Escapes quotes in a string for use in a double quoted CSV string
     * @param value
     * @return
     */
    private static String escapeQuotesForCsv(String value) {
        if (value.contains("\"")) {
            return value.replace("\"", "\"\"");
        } else {
            return value;
        }
    }
}

Related

  1. getCSV(Collection values)
  2. getCsv(Collection collection)
  3. getCSVPhrase(Collection objects)
  4. toCSV(int[] a)
  5. toCsv(Object[] arr, String format)
  6. toCSV3_2(double[][][] array, int index1, int index3)
  7. toCSVBuffer(byte barr[])
  8. toCsvLine(final String[] parts)
  9. toCSVString(double[] d)