Skip to content

Instantly share code, notes, and snippets.

@mousedoc
Last active June 28, 2022 07:14
Show Gist options
  • Save mousedoc/19f4c5054b803ac11ac288a8d52c4dbf to your computer and use it in GitHub Desktop.
Save mousedoc/19f4c5054b803ac11ac288a8d52c4dbf to your computer and use it in GitHub Desktop.
C# - String array to CSV Line
public static string FieldsToLine(string[] fields)
{
if (fields == null || fields.Length <= 0)
return string.Empty;
string line = string.Empty;
foreach (var field in fields)
{
string convertedField = field;
if (convertedField.Contains("\""))
convertedField = convertedField.Replace("\"", "\"\"");
if (convertedField.Contains(",") || convertedField.Contains("\\n"))
convertedField = String.Format("\"{0}\"", convertedField);
line += convertedField + ",";
}
return line.Remove(line.Length - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment