CsvParseAnalyzed Method

Parses a CSV-String after it had been analyzed.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static CsvRecord[] ParseAnalyzed(
	string csv,
	Header header = Header.ProbablyPresent,
	int analyzedLines = 5
)

Parameters

csv  String
The CSV-String to parse.
header  Header  (Optional)
A supposition that is made about the presence of a header row.
analyzedLines  Int32  (Optional)
Maximum number of lines to analyze in csv. The minimum value is AnalyzedLinesMinCount. If csv has fewer lines than analyzedLines, it will be analyzed completely. (You can specify Int32.MaxValue to analyze the entire String in any case.)

Return Value

CsvRecord
An array of CsvRecord objects containing the parsed data.

Remarks

CsvAnalyzer performs a statistical analysis on the String. The result of the analysis is therefore always only an estimate, the accuracy of which increases with the number of lines analyzed.

The field delimiters COMMA (',', %x2C), SEMICOLON (';', %x3B), HASH ('#', %x23), TAB ('\t', %x09), and SPACE (' ', %x20) are recognized automatically.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
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
*/

Exceptions

ArgumentNullExceptioncsv is null.
ArgumentOutOfRangeException

header is not a defined value of the Header enum.

- or -

header is a combination of Header values.

CsvFormatExceptionInvalid CSV file. Try to increase the value of analyzedLines to get a better analyzer result!

See Also