How to split a string on multiple multi‑character delimiters (and keep them) in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static bool IsDelimChar(char c, string[] delims) {
        foreach (var d in delims) {
            if (d.Length > 0 && d[0] == c)
                return true;
        }
        return false;
    }

    static List<string> SplitKeepMultiDelims(string s, string[] delims) {
        var result = new List<string>();
        int i = 0;

        while (i < s.Length) {
            char c = s[i];

            if (IsDelimChar(c, delims)) {
                // Count repeated delimiter characters
                int start = i;
                while (i < s.Length && s[i] == c)
                    i++;

                result.Add(s.Substring(start, i - start));
            }
            else {
                // Collect normal text until next delimiter run
                int start = i;
                while (i < s.Length && !IsDelimChar(s[i], delims))
                    i++;

                result.Add(s.Substring(start, i - start));
            }
        }

        return result;
    }

    static void Main()
    {
        string s = "aa==bbb---cccc++++ddddd";
        string[] delims = { "=", "-", "+" };

        var parts = SplitKeepMultiDelims(s, delims);

        foreach (var p in parts)
            Console.Write($"[{p}] ");
    }
}



/*
run:

[aa] [==] [bbb] [---] [cccc] [++++] [ddddd] 

*/

 



answered Mar 10 by avibootz

Related questions

...