Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

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

...