tondre3na
tondre3na

Reputation: 29

Replace string in string list with string list

I am trying to replace from a list of strings, a found string with multiple strings. This is what I have so far:

private List<string> AddBinList( int CSNum,  List<string> dataLogLines)
    {
        foreach (var line in dataLogLines)
        {
            try
            {
                if (line.Contains("&ALLPASSBINNUMBER&") && line.Contains("&ALLPASSBINNAME&"))
                {
                    List<string> newLines = new List<string>();
                    foreach (var passBin in site[CSNum].Tokens.PassBins)
                    {
                        string outputLine = line.Replace("&ALLPASSBINNUMBER&", passBin.Value.ToString());
                        outputLine = line.Replace("&ALLPASSBINNAME&", passBin.Key);
                        newLines.Add(outputLine);
                    }

                    dataLogLines = dataLogLines.Select(x => x.Replace(line, newLines)).ToList();
                }
            }
            catch { }
        }
        return dataLogLines;
    }

EDITIORS NOTE: The problem the OP is having is

dataLogLines = dataLogLines.Select(x => x.Replace(line, newLines)).ToList();

is giving a compiler error.

Upvotes: 0

Views: 432

Answers (1)

I4V
I4V

Reputation: 35353

Sample input would be a string list like: {"item1","item2"}. Now I want to replace "item2" with new string list, i.e. {"item3","item4"} so my final list looks like {"item1","item3","item4"

List<string> list1 = new List<string>() { "item1", "item2" };
list1 = list1.SelectMany(x => x == "item2" ? new[] { "item3", "item4" } 
                                            : new[] { x })
             .ToList();

Upvotes: 2

Related Questions