C# 如何替换字符串数组中的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2349339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I replace an item in a string array?
提问by Jade M
Using C# how do I replace an item text in a string array if I don't know the position?
如果我不知道位置,使用 C# 如何替换字符串数组中的项目文本?
My array is [berlin, london, paris] how do I replace paris with new york?
我的数组是 [berlin, london, paris] 我如何用纽约替换巴黎?
采纳答案by itowlson
You need to address it by index:
您需要通过索引解决它:
arr[2] = "new york";
Since you say you don't know the position, you can use Array.IndexOf to find it:
既然你说你不知道位置,你可以使用 Array.IndexOf 来找到它:
arr[Array.IndexOf(arr, "paris")] = "new york"; // ignoring error handling
回答by Rob Sedgwick
You could also do it like this:
你也可以这样做:
arr = arr.Select(s => s.Replace("paris", "new york")).ToArray();