Last active
October 22, 2019 15:50
-
-
Save binki/c2f2adc7da7dc771a3f54a22d92cd31e to your computer and use it in GitHub Desktop.
Example of quoting CSV in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is some text, but it is free-form and might have double quotes (") in it | Text with a newline | 23 |
---|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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