Super fast, memory efficient parser
$ dotnet add package NitroTextFieldParserThe 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.
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 | Gen2 | Allocated |
|---|---|---|---|---|---|---|---|---|
| ProcessCsvAsMemoryLineNewTextFieldParser | 1.708 ms | 0.0327 ms | 0.0425 ms | 1 | 310.5469 | 130.8594 | - | 1.35 MB |
| ProcessSimpleCsvOldTextFieldParser | 10.165 ms | 0.1966 ms | 0.2624 ms | 2 | 3343.7500 | 515.6250 | 31.2500 | 12.46 MB |
Stream, suitable for large files.Include the NitroTextFieldParser class in your .NET project or package it into a shared library.
Create an instance of the parser with your data stream.
using NitroTextFieldParser;
using (var stream = File.OpenRead("data.csv"))
using (var parser = new TextFieldParser(stream))
{
parser.SetDelimiters(","); // Set the delimiter for parsing
parser.HasFieldsEnclosedInQuotes = true; // Enable quote handling
}Define the delimiters used to separate fields:
parser.SetDelimiters(",", "\t"); // Supports multiple delimitersRead 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
}
}Enable quote handling for fields:
parser.HasFieldsEnclosedInQuotes = true; // Enable quote handlingExample Input:
"Name","Age","City"
"Alice, Bob","30","New York"Output:
Row 1:
Row 2:
Specify a custom character encoding:
using (var parser = new TextFieldParser(stream, Encoding.UTF8))
{
// Custom encoding setup
}Keep the stream open after parsing:
var parser = new TextFieldParser(stream, Encoding.UTF8, true, true);Read the entire file content at once:
var content = parser.ReadToEnd();Input File (data.csv):
"ID","Name","Email"
1,"John Doe","john@example.com"
2,"Jane Smith","jane@example.com"Output:
Row 1:
Row 2:
Row 3: