Last active
June 28, 2022 07:14
-
-
Save mousedoc/19f4c5054b803ac11ac288a8d52c4dbf to your computer and use it in GitHub Desktop.
C# - String array to CSV Line
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
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