Search code examples
c#tuplesvaluetuple

Convert an array of separated strings into an ValueTuple list


I have an array of strings separated by ; , like this

myArr[0]="string1;string2;string3"
myArr[1]="string4;string5;string6"

how can i convert it into an list of ValueTuple(string,string,string)?

List<(string,string,string)>

Solution

  • You can use Linq like this:

        var myArr= new string[2];
        myArr[0]="string1;string2;string3";
        myArr[1]="string4;string5;string6";
                    
        var tuples = myArr.Select(x=> 
        {
           var separated = x.Split(";");
           return (separated[0],separated[1], separated[2]);
        });
                    
    foreach(var tuple in tuples)
    {
    Console.WriteLine($"{tuple.Item1}, {tuple.Item2}, {tuple.Item3}");
    }