Java CSV String Create toCsvString(String text, String splitString, String replaceSplitChar)

Here you can find the source of toCsvString(String text, String splitString, String replaceSplitChar)

Description

csv value cracch quot if it contain a line-separator.

License

Apache License

Parameter

Parameter Description
text a parameter
splitString a parameter
replaceSplitChar usually use empty to remove splitString

Declaration

public static String toCsvString(String text, String splitString, String replaceSplitChar) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w  w  w.ja v  a  2  s  . co m
     * 
     * csv value cracch quot if it contain a line-separator.
     * and that case quot string must be escaped with additional quot;
     * 
     * can't read this csv from CSVReader
     * 
     * @param text
     * @param splitString
     * @param replaceSplitChar usually use empty to remove splitString
     * @return
     */
    public static String toCsvString(String text, String splitString, String replaceSplitChar) {
        boolean containLineSeparator = text.indexOf("\n") != -1;
        if (!containLineSeparator) {
            containLineSeparator = text.indexOf("\r") != -1;
        }
        text = text.replace(splitString, replaceSplitChar);
        if (containLineSeparator) {
            text = text.replace("\"", "\"\"");
            return "\"" + text + "\"";
        } else {//
            return text;
        }
    }
}

Related

  1. toCSVString(double[] d)
  2. toCsvString(Object object)
  3. toCSVString(Object[] pStringArray)
  4. toCSVString(String s)
  5. toCsvString(String text, char separator, char quote, String quoteEscaped)
  6. toCSVString(String[] fields)