Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string value inside list<>

Tags:

c#

list

split

I have a list contained within a dictionary that contains the follwing values

value a
value b
value c
value 1, value 2, value3
value d

the end result I would like is

value a
value b
value c
value 1
value 2
value 3
value d

problem is iterating through the dictionary and trying to change the collection will not work as I am trying to modify it while looping through it

string[] ar;
        foreach (var kvp in dict)
        {
            if (kvp.Key == "STAR-016")
            {
                foreach (var v in kvp.Value)
                {
                    if (v.Contains(','))
                    {
                        ar = v.Split(',');
                        foreach (var a in ar)
                        {
                            kvp.Value.Add(a);
                        }
                    }
                }
            }
        }

how can i get the desired result?

like image 893
Arianule Avatar asked May 28 '26 00:05

Arianule


2 Answers

Try using LINQ:

var list = new List<string>()
{
    "value a",
    "value b",
    "value 1, value 2, value 3",
    "value c"
};

/* THIS IS THE IMPORTANT PART: */
/* Replace list with kvp.Value */
list = list.SelectMany( 
         i => i.Split( ',' ).Select( v => v.Trim() ) 
       ).ToList();

foreach ( var item in list )
    Console.WriteLine( item );

Output:

value a
value b
value 1
value 2
value 3
value c

To use this within your code:

foreach (var kvp in dict)
{
    if (kvp.Key == "STAR-016")
    {
        var newList = 
            kvp.Value.SelectMany(
                i => i.Split( ',' ).Select( v => v.Trim() )
            );
        kvp.Value.Clear();
        kvp.Value.AddRange(newList);
    }
}

Thanks to @Mudu for pointing out the simpler i => syntax

like image 58
JDB Avatar answered May 30 '26 15:05

JDB


Assuming that this is just an example and you actually want this not only for a key STAR-016 but for all where the value (which is a List<string>) contains a comma:

Dictionary<string, List<String>> dict = new Dictionary<string, List<String>>();
dict.Add("STAR-016", new List<string>() { 
    "value a", "value b", "value c", "value 1, value 2, value 3", "value d"
});

foreach (var kvp in dict)
{
    for (int i = kvp.Value.Count - 1; i >= 0; i--)
    {
        string str = kvp.Value[i];
        if (str.Contains(','))
        {
            var parts = str.Split(',').Select(p => p.Trim());
            kvp.Value.RemoveAt(i);
            kvp.Value.InsertRange(i, parts);
        }
    }
}

Demo

I'm looping from the end to the start to avoid complications because InsertRange will add new strings which increases the Count.
I'm using RemoveAt to replace the strings with commas with new strings(one for each part splitted by comma) which are added. I'm using InsertRange instead of AddRange because you want to keep the order.

Result:

value a
value b
value c
value 1
value 2
value 3
value d
like image 39
Tim Schmelter Avatar answered May 30 '26 14:05

Tim Schmelter