简体   繁体   中英

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.

Sample input would be a string list like: {"item1","item2"}. Now I want to replace "item2" with new string list, ie {"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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM