Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code not really compliant with <a href="http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm" rel="nofollow noreferrer">full CSV format</a>. If you are just generating CSV from data that has no commas, leading/trailing spaces, tabs, newlines or quotes, it should be fine. However, in most real-world data-exchange scenarios, you do need the full imlementation. </p> <p>For generation to proper CSV, you can use this:</p> <pre><code>public static String EncodeCsvLine(params String[] fields) { StringBuilder line = new StringBuilder(); for (int i = 0; i &lt; fields.Length; i++) { if (i &gt; 0) { line.Append(DelimiterChar); } String csvField = EncodeCsvField(fields[i]); line.Append(csvField); } return line.ToString(); } static String EncodeCsvField(String field) { StringBuilder sb = new StringBuilder(); sb.Append(field); // Some fields with special characters must be embedded in double quotes bool embedInQuotes = false; // Embed in quotes to preserve leading/tralining whitespace if (sb.Length &gt; 0 &amp;&amp; (sb[0] == ' ' || sb[0] == '\t' || sb[sb.Length-1] == ' ' || sb[sb.Length-1] == '\t' )) { embedInQuotes = true; } for (int i = 0; i &lt; sb.Length; i++) { // Embed in quotes to preserve: commas, line-breaks etc. if (sb[i] == DelimiterChar || sb[i]=='\r' || sb[i]=='\n' || sb[i] == '"') { embedInQuotes = true; break; } } // If the field itself has quotes, they must each be represented // by a pair of consecutive quotes. sb.Replace("\"", "\"\""); String rv = sb.ToString(); if (embedInQuotes) { rv = "\"" + rv + "\""; } return rv; } </code></pre> <p>Might not be world's most efficient code, but it has been tested. Real world sucks compared to quick sample code :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload