Class TokenBuilder
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
TokenBuilder(char, char, int)
Initializes a new instance of the TokenBuilder class.
public TokenBuilder(char delimiter, char qualifier, int tokens)
Parameters
delimitercharThe delimiter used to separate tokens of this instance.
qualifiercharThe qualifier that surrounds a token.
tokensintThe total number of tokens.
TokenBuilder(string, string, int)
Initializes a new instance of the TokenBuilder class.
public TokenBuilder(string delimiter, string qualifier, int tokens)
Parameters
delimiterstringThe delimiter used to separate tokens of this instance.
qualifierstringThe qualifier that surrounds a token.
tokensintThe total number of tokens.
Exceptions
- FormatException
The length of
delimiteris not 1 -or- The length ofqualifieris not 1.
Properties
Delimiter
Gets the delimiter used to separate tokens of this builder.
public char Delimiter { get; }
Property Value
- char
The delimiter used to separate tokens of this builder.
IsValid
Returns a value indicating whether the current state of this builder is valid.
public bool IsValid { get; }
Property Value
- bool
trueif the current state of this builder is valid; otherwise,false.
Qualifier
Gets the qualifier that surrounds a token of this builder.
public char Qualifier { get; }
Property Value
- char
The qualifier that surrounds a token of this builder.
Tokens
Gets the total number of tokens this builder represents.
public int Tokens { get; }
Property Value
- int
The total number of tokens this builder represents.
Methods
Append(string)
Appends the specified value to this builder.
public TokenBuilder Append(string value)
Parameters
valuestringThe value to tokenize.
Returns
- TokenBuilder
A reference to this instance.
ToString()
Returns a string that represents this instance.
public override string ToString()