Table of Contents

Class DelimitedString

Namespace
Cuemon
Assembly
Cuemon.Core.dll

Provides a set of static methods to convert a sequence into a delimited string and break a delimited string into substrings.

public static class DelimitedString
Inheritance
DelimitedString

Examples

The following example shows how to create a delimited string from an array of integers and split it back into parts using DelimitedString. The delimiter and string converter are configured through setup options.

using System;
using System.Globalization;
using Cuemon;

namespace MyApp.Delimited;

public class DelimitedStringExample
{
    public void Demonstrate()
    {
        var numbers = new[] { 1, 2, 3, 4, 5 };
        string csv = DelimitedString.Create(numbers, o =>
        {
            o.Delimiter = ",";
            o.StringConverter = value => value.ToString();
        });
        Console.WriteLine(csv); // "1,2,3,4,5"

        string[] parts = DelimitedString.Split(csv, o =>
        {
            o.Delimiter = ",";
        });
        Console.WriteLine(string.Join(" | ", parts)); // "1 | 2 | 3 | 4 | 5"
    }
}

Methods

Create<T>(IEnumerable<T>, Action<DelimitedStringOptions<T>>)

Creates a delimited string representation from the specified source.

public static string Create<T>(IEnumerable<T> source, Action<DelimitedStringOptions<T>> setup = null)

Parameters

source IEnumerable<T>

The IEnumerable<T> to convert.

setup Action<DelimitedStringOptions<T>>

The DelimitedStringOptions<T> which may be configured.

Returns

string

A string of delimited values that is a result of source.

Type Parameters

T

Exceptions

ArgumentNullException

source cannot be null.

Split(string, Action<DelimitedStringOptions>)

Splits the specified value into substrings by using the configured Delimiter and Qualifier.

public static string[] Split(string value, Action<DelimitedStringOptions> setup = null)

Parameters

value string

The delimited string to split.

setup Action<DelimitedStringOptions>

The DelimitedStringOptions which may be configured.

Returns

string[]

An array of string values that contains the substrings extracted from value.

Remarks

The default implementation conforms to RFC 4180.

This implementation was inspired by the following Stack Overflow discussions:

  • https://stackoverflow.com/questions/2807536/split-string-in-c-sharp
  • https://stackoverflow.com/questions/3776458/split-a-comma-separated-string-with-both-quoted-and-unquoted-strings
  • https://stackoverflow.com/questions/6542996/how-to-split-csv-whose-columns-may-contain

Exceptions

ArgumentException

value is null, empty, or consists only of white-space characters.

InvalidOperationException

Thrown when value cannot be split using the configured Delimiter and Qualifier. This typically indicates malformed input, such as an unclosed qualified field.