0

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.

9
  • 1
    What problem are you having? Also why are you catching all exceptions, it is likely hiding whatever is causing the program to not work. Commented Jun 3, 2013 at 21:10
  • the catch is for handling keys not present in dictionary. The replace is what doesn't work. I want to be able to replace from a list of strings, one string with a new list of multiple strings Commented Jun 3, 2013 at 21:15
  • 1
    I want to be able to replace from a list of strings, one string with a new list of multiple strings???? How about posting a sample input, expected output and a brief description? Commented Jun 3, 2013 at 21:15
  • This line looks like its going to be a problem, since you are modifying the dataLogLines variable while iterating over it. --> dataLogLines = dataLogLines.Select(x => x.Replace(line, newLines)).ToList(); Commented Jun 3, 2013 at 21:19
  • 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"} Commented Jun 3, 2013 at 21:21

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.