Jade M
Jade M

Reputation: 3101

How do I replace an item in a string array?

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?

Upvotes: 24

Views: 88052

Answers (2)

Rob Sedgwick
Rob Sedgwick

Reputation: 4514

You could also do it like this:

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

Upvotes: 13

itowlson
itowlson

Reputation: 74802

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

Upvotes: 38

Related Questions