How do I replace an item in a string array?

后端 未结 2 714
甜味超标
甜味超标 2021-02-13 11:43

Using C# how do I replace an item text in a string array if I don\'t know the position?

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

相关标签:
2条回答
  • 2021-02-13 12:04

    You could also do it like this:

    arr = arr.Select(s => s.Replace("paris", "new york")).ToArray();
    
    0 讨论(0)
  • 2021-02-13 12:12

    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
    
    0 讨论(0)
提交回复
热议问题