C#Lambda表达式替换字符串数组中的字符串值

lma*_*zur 3 .net c# lambda

我试图使用.net 4.0 lambda方法删除数组中项目的双引号(").这是我的代码,但是,这似乎不起作用.

我究竟做错了什么?

string[] sa = new string[] { "\"Hello\"", "\"Goodbye\"", "\"Moshi\"", "\"Adios\"" };

// Trying to replace the 
Array.ForEach(sa, s => s.Replace("\"", "")); // Doesn't remove the quotes surrounding the string "Hello".
foreach(var s in sa)
   Console.WriteLine(s);
Run Code Online (Sandbox Code Playgroud)

这仍然没有摆脱"围绕物品.

das*_*ght 8

没有lambda表达式可以插入ForEach来实现你的目标,因为lambda可以采取的动作没有对元素的写访问权,并且它string本身是不可变的.

您可以做的是替换整个数组,如下所示:

sa = sa.Select(s => s.Replace("\"", "")).ToArray();
Run Code Online (Sandbox Code Playgroud)

这种方法有效,因为它sa用一个新创建的基于sa元素的数组替换整个数组.