-
Notifications
You must be signed in to change notification settings - Fork 16
Open
Description
This one from stackoverflow is good:
public static class CsvTextFileFormatter
{
// https://stackoverflow.com/questions/6377454/escaping-tricky-string-to-csv-format
public static string FormatCsvCell(char separator, string cell, bool alwaysQuote = false)
{
bool mustQuote(string cell) => cell.IndexOfAny(new char[] { separator, '"', '\r', '\n' }) > -1;
if (alwaysQuote || mustQuote(cell))
{
StringBuilder sb = new StringBuilder();
sb.Append('\"');
foreach (char nextChar in cell)
{
sb.Append(nextChar);
if (nextChar == '"')
sb.Append('\"');
}
sb.Append('\"');
return sb.ToString();
}
return cell;
}
public static string FormatCsvRow(char separator, IEnumerable<string> cells, bool alwaysQuote = false)
{
return string.Join(separator, values.Select(cell => FormatCsvCell(separator, cell, alwaysQuote)));
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels