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?
To replace an element in an array:Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.
To replace an element in Java ArrayList, set() method of java. util. An ArrayList class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element.
How do you replace text from an array using vb.net? $str = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. '; $find = array('Lorem', 'sit', 'elit'); $replace = str_replace($find, '', $str);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With