CsvRecordExtensionFillWith(CsvRecord, ReadOnlySpanReadOnlyMemoryChar, Boolean) Method

Fills record with the items of a read-only span of ReadOnlyMemory<Char> values.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static void FillWith(
	this CsvRecord record,
	ReadOnlySpan<ReadOnlyMemory<char>> data,
	bool resetExcess = true
)

Parameters

record  CsvRecord
The CsvRecord instance to be filled.
data  ReadOnlySpanReadOnlyMemoryChar
The ReadOnlyMemory<Char> values with which to fill record.
resetExcess  Boolean  (Optional)

If data has fewer items than record fields and this parameter is true, the surplus fields in record will be reset to the default value .

For performance reasons this parameter can be set to false when writing CSV because WriteRecord resets all fields in record.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type CsvRecord. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
using System.Text;
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class CsvAnalyzerExample
{
    public static void ParseForeignCsvFile(string filePath)
    {
        const string nonStandardCsv = """


            First # "Second # Column"   
            1,"2",3 # "Get's
            too much" # LOST?

            too few


            """;

        File.WriteAllText(filePath, nonStandardCsv, Encoding.Unicode);

        using CsvReader csv = Csv.OpenReadAnalyzed(filePath);
        CsvRecord[] data = [.. csv];

        using (CsvWriter writer = Csv.OpenWrite(filePath, data[0].ColumnNames))
        {
            foreach (CsvRecord record in data)
            {
                writer.Record.FillWith(record.Values);
                writer.WriteRecord();
            }
        }

        Console.WriteLine(File.ReadAllText(filePath));
    }
}

/*
 Console output:

Column1,Column2,Column3
First ,Second # Column,
"1,""2"",3 ","Get's
too much", LOST?
too few,,

*/

Exceptions

ArgumentNullExceptionrecord is null.
ArgumentOutOfRangeExceptiondata contains more items than record.

See Also