[C#] Split and keep delimiters at the start of each sections RRS feed

  • Question

  • Hi ! I'm doing a c# application and I need to split user inputs.

    If the user entre something like "-push $a 75 $c 2 -wait $t 100", the output should be this array : [ "-push $a 75 $c 2", "-wait $t 10" ].

    Not that I have all delimiters in an array : [ "-push", "-wait", "-cursor", ...]

    I tried searching "Split and keep delimiters c#" but none of the results were good.So, if somebody feels to make me a small code for it, I would be satisfied.

    Thanks for reading !

    Note : I'm french, sorry for my bad english >:(

    • Edited by Scxr Saturday, December 1, 2018 12:32 PM
    Saturday, December 1, 2018 11:24 AM

All replies

  • hello,

    here is an example that <g class="gr_ gr_37 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="37" id="37">splita</g> a string using a delimiter, which is appended to each item through the Select() LINQ projection method

    public static List<string> splitAndKeepDelimiters(char delimiter, string str)
         return str.Split(delimiter).Select(s => $"{delimiter}{s}");

    but the question is how to deal with parameters values that contain the delimiter character!!!

    for example:

    -push $a 75 $c 2 -wait $t 100 -out "---test---/r/nsuccess!"

    I think you need a parsing mechanism or some sort of regex parser.

    hope it helps!

    Good Coding;

    Saturday, December 1, 2018 1:55 PM
  • Check an example:

    string input = "-push $a 75 $c 2 -wait $t 100";
    string[] keywords = new[] { "push", "wait", "cursor" };
    string k = string.Join( "|", keywords.Select( Regex.Escape ) );
    string p = $@"-({k})((?!\s*-({k})).)*";
    string[] output = Regex.Matches( input, p, RegexOptions.IgnoreCase ).Cast<Match>().Select( m => m.Value ).ToArray();

    Saturday, December 1, 2018 2:20 PM

  • esperant que ca aide.

    As this is an English language forum all replies should be in English.

    If posters begin writing replies in whatever language is most familiar
    to an OP (or other participants), the forum could become a jumble of
    mixed-language posts. That would quickly reduce the communication 
    effectiveness of the forum, and require followers to wade through
    countless incomprehensible posts. That would discourage active and 
    frequent participation.

    Note that the same applies to all forums in other languages, not just to 
    the English language forums. Posts should be in the native language
    designated for that particular forum.

    - Wayne

    Saturday, December 1, 2018 2:35 PM
  • I totally agree!

    it was a reaction to the language switch in my cognitive perception, a kind of type inferred return type, LoL!

    I'll pay attention for future replies ;)

    Saturday, December 1, 2018 3:41 PM
  • Hi Scxr,

    Thank you for posting here.

    Please note that this is an English forum, it would be better to post in English.

    For your question, you could try this code below.

    using System;
    using System.Collections.Generic;
    namespace test2
        class Program
            static void Main(string[] args)
                string input = "-push $a 75 $c 2 -wait $t 100";
                string[] output = input.Split('-');
                List<string> list = new List<string>();
                string[] newput = list.ToArray();
                foreach (var item in newput)


    Best regards,

    Jack J Jun.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, December 3, 2018 7:36 AM
  • Hi Scxr

    find below snippet and let me know if you have any comments

     string input = "-push $a 75 $c 2 -wait $t 100 -cursor $c $d -push #8 $10";
                string[] delim = new string[] { "-wait", "-push", "-cursor" };
                string[] splitData= input.Split(delim,StringSplitOptions.RemoveEmptyEntries);
                List<string> result = new List<string>();
                foreach (string data in splitData)
                    foreach (string del in delim)
                        string pattern = del + data;
                        var match = input.Split(pattern);
                        if (match.Length > 1)
                result.ForEach(p => Console.WriteLine(p));

    Tuesday, December 4, 2018 10:19 AM