2013-06-03 165 views
0

我想從字符串列表中替換,找到的字符串與多個字符串。 這是我到目前爲止有:用字符串列表替換字符串列表中的字符串

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注:的OP是遇到的問題是

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

是給編譯器錯誤。

+1

什麼你有問題嗎?另外你爲什麼要捕捉所有的異常,它可能會隱藏任何導致程序無法工作的東西。 –

+0

該捕獲是用於處理字典中不存在的鍵。替換是不起作用的。我希望能夠從一個字符串列表中替換一個字符串和一個新的多個字符串列表 – tondre3na

+1

'我希望能夠從一個字符串列表中替換,一個字符串和一個新的多個字符串列表'??? ?如何發佈樣本輸入,預期輸出和簡要說明? – I4V

回答

2

樣品輸入將是一個字符串列表,如:{ 「物品1」, 「ITEM2」}。現在我要替換「ITEM2」新的字符串列表,即{「項目3」,「ITEM4」},所以我最終名單看起來像{「物品1」,「項目3」,「ITEM4」

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