Search code examples
c#splitstring-parsing

Efficiently split a string in format "{ {}, {}, ...}"


I have a string in the following format.

string instance = "{112,This is the first day 23/12/2009},{132,This is the second day 24/12/2009}"

private void parsestring(string input)
{
    string[] tokens = input.Split(','); // I thought this would split on the , seperating the {}
    foreach (string item in tokens)     // but that doesn't seem to be what it is doing
    {
       Console.WriteLine(item); 
    }
}

My desired output should be something like this below:

112,This is the first day 23/12/2009
132,This is the second day 24/12/2009

But currently, I get the one below:

{112
This is the first day 23/12/2009
{132
This is the second day 24/12/2009

I am very new to C# and any help would be appreciated.


Solution

  • Well, if you have a method that is called ParseString, it's a good thing it returns something (and it might not be that bad to say that it is ParseTokens instead). So if you do that, you can come to the following code

    private static IEnumerable<string> ParseTokens(string input)
    {
        return input
            // removes the leading {
            .TrimStart('{')
            // removes the trailing }
            .TrimEnd('}')
            // splits on the different token in the middle
            .Split( new string[] { "},{" }, StringSplitOptions.None );
    }
    

    The reason why it didn't work for you before, is because your understanding of how the split method works, was wrong, it will effectively split on all , in your example.

    Now if you put this all together, you get something like in this dotnetfiddle

    using System;
    using System.Collections.Generic;
    
    public class Program
    {
        private static IEnumerable<string> ParseTokens(string input)
        {
            return input
                // removes the leading {
                .TrimStart('{')
                // removes the trailing }
                .TrimEnd('}')
                // splits on the different token in the middle
                .Split( new string[] { "},{" }, StringSplitOptions.None );
        }
    
        public static void Main()
        {
            var instance = "{112,This is the first day 23/12/2009},{132,This is the second day 24/12/2009}";
            foreach (var item in ParseTokens( instance ) ) {
                Console.WriteLine( item );
            }
        }
    }