public static string ToCsv(
this IEnumerable<IEnumerable<Object?>?> data,
char delimiter = ',',
IFormatProvider? formatProvider = null,
string? format = null
)
<ExtensionAttribute>
Public Shared Function ToCsv (
data As IEnumerable(Of IEnumerable(Of Object)),
Optional delimiter As Char = ","C,
Optional formatProvider As IFormatProvider = Nothing,
Optional format As String = Nothing
) As String
public:
[ExtensionAttribute]
static String^ ToCsv(
IEnumerable<IEnumerable<Object^>^>^ data,
wchar_t delimiter = L',',
IFormatProvider^ formatProvider = nullptr,
String^ format = nullptr
)
[<ExtensionAttribute>]
static member ToCsv :
data : IEnumerable<IEnumerable<Object>> *
?delimiter : char *
?formatProvider : IFormatProvider *
?format : string
(* Defaults:
let _delimiter = defaultArg delimiter ','
let _formatProvider = defaultArg formatProvider null
let _format = defaultArg format null
*)
-> string
The provider to use to format the value.
- or -
A null reference for InvariantCulture.
A format String to use for all items that implement IFormattable.
- or -
A null reference to use the default format for each item.
For serialization ToString(String, IFormatProvider) is used if the item implements IFormattable, otherwise ToString.
using FolkerKinzel.CsvTools;
namespace Examples;
internal static class CsvStringExample
{
public static void ConvertingCsvStrings()
{
object?[][] data =
[
[ "Color", "Direction", "Number"],
[ "brown", "right", 0],
["red", "left", 42],
[null, "Somewhere \"over\" the Rainbow"],
["""
Blue
in
Green
""", "Up, or down", -3.14]
];
string csv = data.ToCsv();
Console.WriteLine(csv);
Console.WriteLine();
CsvRecord[] results = Csv.ParseAnalyzed(csv);
foreach (CsvRecord record in results)
{
Console.WriteLine(record);
}
}
}
/*
Console Output:
Color,Direction,Number
brown,right,0
red,left,42
,"Somewhere ""over"" the Rainbow",
"Blue
in
Green","Up, or down",-3.14
Color: brown, Direction: right, Number: 0
Color: red, Direction: left, Number: 42
Color: , Direction: Somewhere "over" the Rainbow, Number:
Color: Blue
in
Green, Direction: Up, or down, Number: -3.14
*/
ArgumentNullException | data is null. |
ArgumentOutOfRangeException | delimiter is either the double quotes " or a line break character ('\r' or '\n'). |