开发者

How do I replace an item in a string array?

开发者 https://www.devze.com 2022-12-21 06:12 出处:网络
Using C# how do I replace an item text in a string array if I d开发者_运维百科on\'t know the position?

Using C# how do I replace an item text in a string array if I d开发者_运维百科on't know the position?

My array is [berlin, london, paris] how do I replace paris with new york?


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:

arr[Array.IndexOf(arr, "paris")] = "new york";  // ignoring error handling


You could also do it like this:

arr = arr.Select(s => s.Replace("paris", "new york")).ToArray();
0

精彩评论

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