我用Java写过。
public class CSVUtil {
public static String addQuote(
String pValue) {
if (pValue == null) {
return null;
} else {
if (pValue.contains("\"")) {
pValue = pValue.replace("\"", "\"\"");
}
if (pValue.contains(",")
|| pValue.contains("\n")
|| pValue.contains("'")
|| pValue.contains("\\")
|| pValue.contains("\"")) {
return "\"" + pValue + "\"";
}
}
return pValue;
}
public static void main(String[] args) {
System.out.println("ab\nc" + "|||" + CSVUtil.addQuote("ab\nc"));
System.out.println("a,bc" + "|||" + CSVUtil.addQuote("a,bc"));
System.out.println("a,\"bc" + "|||" + CSVUtil.addQuote("a,\"bc"));
System.out.println("a,\"\"bc" + "|||" + CSVUtil.addQuote("a,\"\"bc"));
System.out.println("\"a,\"\"bc\"" + "|||" + CSVUtil.addQuote("\"a,\"\"bc\""));
System.out.println("\"a,\"\"bc" + "|||" + CSVUtil.addQuote("\"a,\"\"bc"));
System.out.println("a,\"\"bc\"" + "|||" + CSVUtil.addQuote("a,\"\"bc\""));
}
}