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
sourceIEnumerable<T>The IEnumerable<T> to convert.
setupAction<DelimitedStringOptions<T>>The DelimitedStringOptions<T> which may be configured.
Returns
Type Parameters
T
Exceptions
- ArgumentNullException
sourcecannot be null.
Split(string, Action<DelimitedStringOptions>)
public static string[] Split(string value, Action<DelimitedStringOptions> setup = null)
Parameters
valuestringThe delimited string to split.
setupAction<DelimitedStringOptions>The DelimitedStringOptions which may be configured.
Returns
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
valueis null, empty, or consists only of white-space characters.- InvalidOperationException
Thrown when
valuecannot be split using the configured Delimiter and Qualifier. This typically indicates malformed input, such as an unclosed qualified field.