Skip to content

Instantly share code, notes, and snippets.

@binki
Last active October 22, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binki/c2f2adc7da7dc771a3f54a22d92cd31e to your computer and use it in GitHub Desktop.
Save binki/c2f2adc7da7dc771a3f54a22d92cd31e to your computer and use it in GitHub Desktop.
Example of quoting CSV in C#
This is some text, but it is free-form and might have double quotes (") in it Text with a newline 23

Imported into Excel: imported into Excel

using System;
public static class Program {
public static void Main() {
var num = 23;
var value = "This is some text, but it is free-form and might have double quotes (\") in it";
var value2 = "Text with a\r\nnewline";
Console.WriteLine(QuoteCsvString(value) + "," + QuoteCsvString(value2) + "," + num);
}
public static string QuoteCsvString(string value) {
return "\"" + value.Replace("\"", "\"\"") + "\"";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment