开发者

Replace a substring in each element of a string array?

Hey, I have an array of strings and I want to replace a certain substring in each of those elements. Is there an easy way 开发者_运维技巧to do that besides iterating the array explicitly?

Thanks :-)


Ultimately, anything you do is going to do exactly that anyway. A simple for loop should be fine. There are pretty solutions involving lambdas, such as Array.ConvertAll / Enumerable.Select, but tbh it isn't necessary:

for(int i = 0 ; i < arr.Length ; i++) arr[i] = arr[i].Replace("foo","bar");

(the for loop has the most efficient handling for arrays; and foreach isn't an option due to mutating the iterator variable)


You could iterate the array implicitly

arrayOfStrings = arrayOfStrings.Select(s => s.Replace("abc", "xyz")).ToArray();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜