List Converter — C# source
Transform a list between separators (newline, comma, space, pipe, semicolon, tab) with trim, dedupe, sort, and empty-removal options. Runs entirely in your browser, with a shareable link.
This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// List Converter — split/trim/dedup/sort/join between separators — C# (.NET 8) port of the list-converter tool.
using System;
using System.Linq;
namespace CosmoDev.ListConverter;
public static class ListConverter
{
// Resolve a TS-style separator name to its glue; unknown names pass
// through as a literal separator (the TS resolveSep behavior).
static string ResolveSep(string? name, string fallback) => name switch
{
"newline" => "\n",
"comma" => ",",
"space" => " ",
"pipe" => "|",
"semicolon" => ";",
"tab" => "\t",
null => fallback,
_ => name,
};
// Options mirror the TS ListOptions; defaults are from=newline, to=comma.
public sealed record Options
{
public string? From { get; init; } = "newline";
public string? To { get; init; } = "comma";
public bool Trim { get; init; }
public bool RemoveEmpty { get; init; }
public bool Unique { get; init; }
public bool Sort { get; init; }
public bool CaseInsensitive { get; init; }
}
// Convert a list between separators: split -> (trim) -> (drop empties) ->
// (dedup, first occurrence wins) -> (stable sort) -> join. Mirrors the Go twin.
public static string Convert(string input, Options? options = null)
{
var o = options ?? new Options();
var items = input.Split(ResolveSep(o.From, "\n"));
string to = ResolveSep(o.To, ",");
if (o.Trim) items = items.Select(s => s.Trim()).ToArray();
if (o.RemoveEmpty) items = items.Where(s => s.Length != 0).ToArray();
if (o.Unique) // GroupBy yields groups in first-seen order; First()
// keeps the first occurrence's original casing.
items = items
.GroupBy(s => o.CaseInsensitive ? s.ToLowerInvariant() : s)
.Select(g => g.First())
.ToArray();
if (o.Sort) // OrderBy is documented stable; the key lowercases only
// for comparison, leaving the stored values untouched.
items = items
.OrderBy(s => o.CaseInsensitive ? s.ToLowerInvariant() : s,
StringComparer.Ordinal)
.ToArray();
return string.Join(to, items);
}
public static void Main()
{
// "b\n a \nB\na\n\nc" -> trim, drop empties, case-insensitive dedup+sort
var o = new Options
{
Trim = true,
RemoveEmpty = true,
Unique = true,
Sort = true,
CaseInsensitive = true,
};
Console.WriteLine(Convert("b\n a \nB\na\n\nc", o)); // a,b,c
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →