0

Updated: Thank you for the answer, but I disagree that my question is answered by another thread. "Multiple delimiters" and "Multi-Character delimiters" are 2 different questions.

This is my code so far:

        List<string> delimiters = new List<string>();

        List<string> data = new List<string> 
        {
        "Car|cBlue,Mazda~Model|m3",
        //More data
        };

        string userInput = "";
        int i = 1;

        //The user can enter a maximum of 5 delimiters
        while (userInput != "go" && i <= 5)
        {
            userInput = Console.ReadLine();
            delimiters.Add(userInput);
            i++;
        }

        foreach (string delimiter in delimiters)
        {
            foreach (string s in data)
            {
                //This split is not working
                //string output[] = s.Split(delimiter);
            }
        }

So, if the user enters "|c" and "~", the expected output is: "Car", "Blue,Mazda", "Model|m3"

If the user enters "|c", "|m", and ",", then the expected output will be: "Car", "Blue", "Mazda~Model", "3"

Unnie
  • 918
  • 8
  • 30
C.J.
  • 3,409
  • 8
  • 34
  • 51
  • possible duplicate of [How do I split a string by a multi-character delimiter in C#?](http://stackoverflow.com/questions/1126915/how-do-i-split-a-string-by-a-multi-character-delimiter-in-c) – sab669 Mar 18 '14 at 18:42
  • I don't understand why you can't use the array that split already takes. – rerun Mar 18 '14 at 18:43
  • 1
    @sab669 Multiple delimiters and multi-character delimiters are 2 totally different questions. – C.J. Mar 18 '14 at 19:09

3 Answers3

3

String.Split has an overload that does exactly that - you just need to convert your List<string> to a string[] :

string input = "Car|cBlue,Mazda~Model|m3";
List<string> delims = new List<string> {"|c", "~"};

string[] out1 = input.Split(delims.ToArray(),StringSplitOptions.None);

//output: 
//    Car 
//    Blue,Mazda 
//    Model|m3 

delims = new List<string> {"|c", "|m", ","};

string[] out2 = input.Split(delims.ToArray(),StringSplitOptions.None).Dump();

//output:
//    Car 
//    Blue 
//    Mazda~Model 
//    3 
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • It doesn't work... I guess my question is more towards "How to assign "dynamic" delimiters" because I cannot hardcode the delimiters. User have to specify them – C.J. Mar 18 '14 at 18:47
  • @C.J. see my edit - just take your list and convert it to a string array. – D Stanley Mar 18 '14 at 18:47
3

Add the user input into the List delimiters.

 string data = "Car|cBlue,Mazda~Model|m3";
            List<string> delimiters = new List<string>();
            delimiters.Add("|c");//Change this to user input
            delimiters.Add("|m");//change this to user input

            string[] parts = data.Split(delimiters.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in parts)
            {
                Console.WriteLine(item);   
            }
Unnie
  • 918
  • 8
  • 30
  • THe problem is, I cannot hardcode the delimiters because they are being specified by the user – C.J. Mar 18 '14 at 18:45
  • Edited the answer to your requirement. Just add logic to get all the user inputs into the List object – Unnie Mar 18 '14 at 18:54
2

You can use SelectMany to get the result from all the data strings and ToArray() method to create an array from delimiters

var result = data.SelectMany(s => s.Split(delimiters.ToArray(), StringSplitOptions.None));
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44