FlexableCsvParser 2.0.0

dotnet add package FlexableCsvParser --version 2.0.0
                    
NuGet\Install-Package FlexableCsvParser -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FlexableCsvParser" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FlexableCsvParser" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="FlexableCsvParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FlexableCsvParser --version 2.0.0
                    
#r "nuget: FlexableCsvParser, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FlexableCsvParser@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FlexableCsvParser&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=FlexableCsvParser&version=2.0.0
                    
Install as a Cake Tool

FlexableCsvParser

A high-performance, flexible, and zero-allocation oriented CSV parser for modern .NET.

Key Features

  • ๐Ÿš€ High Performance: Designed with performance in mind, utilizing Span<char>, ReadOnlySpan<char>, and StringPool to minimize allocations and maximize throughput.
  • ๐Ÿ› ๏ธ Extremely Flexible: Support for custom delimiters of any length for fields, records, quotes, and escape sequences.
  • โœ… RFC 4180 Compliant: Full support for RFC 4180 by default.
  • ๐Ÿงน Configurable Trimming: Built-in support for leading and trailing white space trimming.
  • ๐Ÿ›ก๏ธ Robust Error Handling: Configurable behavior for handling incomplete records (Throw, Fill, or Truncate).
  • ๐Ÿงฉ Async Support: Provides asynchronous APIs for non-blocking I/O.
  • ๐Ÿงถ String Interning: Integrated StringPool to reduce memory footprint by interning repeated field values.

Installation

Install via NuGet:

dotnet add package FlexableCsvParser

Quick Start

The simplest way to get started is using the default RFC 4180 configuration.

using System.IO;
using FlexableCsvParser;

const string csv = "123, \"456 ,\"\"789\"\"\" ,ABC";
int expectedFieldCount = 3;

// Initialize the parser with a TextReader and expected field count
var parser = new CsvParser(new StringReader(csv), expectedFieldCount);

string[] record = new string[expectedFieldCount];

// Read records into a span or array
while (parser.ReadRecord(record) > 0)
{
    Console.WriteLine($"Field 1: {record[0]}");
    Console.WriteLine($"Field 2: {record[1]}");
    Console.WriteLine($"Field 3: {record[2]}");
}

Advanced Usage

Custom Delimiters

FlexableCsvParser excels at handling non-standard CSV formats with multi-character delimiters.

const string csv = "123<Field> <Quote>456<Field>789<Quote><Field>ABC<Record>";

var config = new CsvParserConfig(
    field: "<Field>",
    endOfRecord: "<Record>",
    quote: "<Quote>",
    escape: "<Quote><Quote>"
);

var parser = new CsvParser(new StringReader(csv), 3, config);

Configuration Options

The CsvParserConfig class provides several options to tune the parser's behavior:

Option Description Default
Delimiters Defines the field, record, quote, and escape sequences. Delimiters.Rfc4180
IncompleteRecordHandling How to handle records with fewer fields than expected. ThrowException
WhiteSpaceTrimming Whether to trim leading or trailing white space. None
StringCacheMaxLength Maximum length of strings to be cached in the StringPool. 128

Incomplete Record Handling

You can control how the parser reacts when it encounters a record that doesn't match the expected field count.

var config = new CsvParserConfig
{
    IncompleteRecordHandling = IncompleteRecordHandling.FillInWithNull
};

Available modes:

  • ThrowException: Throws an InvalidDataException.
  • FillInWithEmpty: Fills missing fields with string.Empty.
  • FillInWithNull: Fills missing fields with null.
  • TruncateRecord: Returns only the fields that were successfully read.

White Space Trimming

Trimming can be applied to unquoted and quoted fields.

var config = new CsvParserConfig
{
    WhiteSpaceTrimming = WhiteSpaceTrimming.Both
};

Async Support

For high-throughput applications, use the async API:

string[] record = new string[3];
while (await parser.ReadRecordAsync(record) > 0)
{
    // Process record
}

Performance Note

FlexableCsvParser is built on top of Tokensharp and utilizes modern .NET features to ensure minimal overhead. By using Span<string> for record output and an internal StringPool, it significantly reduces the GC pressure typically associated with CSV parsing.

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 95 5/18/2026
1.1.0 652 2/19/2022
1.0.0 602 2/15/2022