Table of Contents

TokenBuilder Class

Definition

Namespace
Cuemon.Data
Assemblies
Cuemon.Data.dll
Source
src/Cuemon.Data/TokenBuilder.cs

Represents a mutable string of characters optimized for tokens. This class cannot be inherited.

public sealed class TokenBuilder
Inheritance
TokenBuilder

Examples

The following example demonstrates how to use TokenBuilder to build delimited token strings with support for quoted fields.

using System;
using Cuemon.Data;

namespace MyApp.Examples;

public class Example
{
    public void Run()
    {

        // Build a token string with 4 fields, comma-delimited, double-quote qualified
        var builder = new TokenBuilder(',', '"', 4);

        builder.Append("Alice");
        builder.Append(",30,");
        builder.Append("New York");

        Console.WriteLine($"Is valid: {builder.IsValid}");  // false - only 3 of 4 tokens
        Console.WriteLine($"Current:  '{builder}'");

        // Append the last field to complete the token row
        builder.Append("Engineer");
        Console.WriteLine($"Is valid: {builder.IsValid}");  // true
        Console.WriteLine($"Complete: '{builder}'");

        // TokenBuilder is used internally by DsvDataReader for multi-line quoted fields
        // It accumulates input until the expected number of tokens is reached
        var csvBuilder = new TokenBuilder(';', '\"', 3);
        csvBuilder.Append("Product A");
        csvBuilder.Append("Description with ; semicolon");
        csvBuilder.Append("$49.99");

        Console.WriteLine($"CSV line: '{csvBuilder}'");
        Console.WriteLine($"Tokens:   {csvBuilder.Tokens}");

}
}

Constructors

Name Description
TokenBuilder(char, char, int)

Initializes a new instance of the TokenBuilder class.

TokenBuilder(string, string, int)

Initializes a new instance of the TokenBuilder class.

Properties

Name Description
Delimiter

Gets the delimiter used to separate tokens of this builder.

IsValid

Returns a value indicating whether the current state of this builder is valid.

Qualifier

Gets the qualifier that surrounds a token of this builder.

Tokens

Gets the total number of tokens this builder represents.

Methods

Name Description
Append(string)

Appends the specified value to this builder.

ToString()

Returns a string that represents this instance.