NitroTextFieldParser 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package NitroTextFieldParser --version 1.0.2                
NuGet\Install-Package NitroTextFieldParser -Version 1.0.2                
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="NitroTextFieldParser" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NitroTextFieldParser --version 1.0.2                
#r "nuget: NitroTextFieldParser, 1.0.2"                
#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.
// Install NitroTextFieldParser as a Cake Addin
#addin nuget:?package=NitroTextFieldParser&version=1.0.2

// Install NitroTextFieldParser as a Cake Tool
#tool nuget:?package=NitroTextFieldParser&version=1.0.2                

NitroTextFieldParser

Overview

The NitroTextFieldParser is a blazing-fast .NET library for parsing delimited or fixed-width text files. Built for high performance, it minimizes memory usage by leveraging ReadOnlyMemory<char> and supports advanced features such as quoted field handling. Whether you're processing CSVs or custom delimited files, NitroTextFieldParser provides robust and efficient parsing.


Benchmark Results

The NitroTextFieldParser outperforms the built-in TextFieldParser in terms of speed and memory usage. The following benchmark results demonstrate the performance benefits of using the NitroTextFieldParser when processing 1000Rows x 9Columns CSV data:

Method Mean Error StdDev Rank Gen0 Gen1 Allocated
UsingNitroTextFieldParser 2.379 ms 0.0471 ms 0.0595 ms 1 308.5938 125.0000 1386.23 KB
UsingOldTextFieldParser 14.922 ms 0.2980 ms 0.5060 ms 2 3312.5000 437.5000 12790.58 KB

Features

  • High Performance: Minimal memory footprint with direct memory access.
  • Flexible Delimiters: Supports single or multiple custom delimiters.
  • Quoted Field Handling: Handles fields enclosed in quotes with support for escaped quotes.
  • Stream-Based: Reads data directly from any Stream, suitable for large files.
  • Encoding Support: Works with any character encoding.

Installation

Include the NitroTextFieldParser class in your .NET project or package it into a shared library.


Usage

1. Initialize the Parser

Create an instance of the parser with your data stream.

using (var stream = File.OpenRead("data.csv"))
using (var parser = new NitroTextFieldParser(stream))
{
    parser.SetDelimiters(","); // Set the delimiter for parsing
    parser.HasFieldsEnclosedInQuotes = true; // Enable quote handling
}

2. Set Delimiters

Define the delimiters used to separate fields:

parser.SetDelimiters(",", "\t"); // Supports multiple delimiters

3. Parse Data'

Read and parse fields line by line:

while (!parser.EndOfData)
{
    var fields = parser.ReadFields(); // Returns ReadOnlyMemory<char>[] for efficient access
    foreach (var field in fields)
    {
        Console.WriteLine(field.ToString()); // Convert to string for display
    }
}

4. Handle Quoted Fields

Enable quote handling for fields:

parser.HasFieldsEnclosedInQuotes = true; // Enable quote handling

Example Input:

"Name","Age","City"
"Alice, Bob","30","New York"

Output:

Row 1:

  • Name
  • Age
  • City

Row 2:

  • Alice, Bob
  • 30
  • New York

Custom Encoding

Specify a custom character encoding:

using (var parser = new NitroTextFieldParser(stream, Encoding.UTF8))
{
    // Custom encoding setup
}

Stream Control

Keep the stream open after parsing:

var parser = new NitroTextFieldParser(stream, Encoding.UTF8, true, true);

Read Full Content

Read the entire file content at once:

var content = parser.ReadToEnd();

Example Input and Output

Input File (data.csv):

"ID","Name","Email"
1,"John Doe","john@example.com"
2,"Jane Smith","jane@example.com"

Output:

Row 1:

  • ID
  • Name
  • Email

Row 2:

  • 1
  • John Doe
  • john@example.com

Row 3:

  • 2
  • Jane Smith
  • jane@example.com

Why Use NitroTextFieldParser?

  • Efficient memory usage for handling large files.
  • Flexible support for custom delimiters and quoted fields.
  • Easy to integrate into any .NET project.

License

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

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
1.0.3 62 1/3/2025
1.0.2 72 12/31/2024