C # String Array Replace last element

I have a String Array that comes from a split line

string[] newName= oldName.Split('\\');

newName.Last().Replace(newName.Last(), handover);

Why doesn't this replace my last element in an array?

last () comes from using linq

considers

+3
source share
1 answer

A call string.Replacedoes not change existing rows — rows are immutable.

Instead, it returns a new line with appropriate replacements. However, you are not using the return value, so basically it is non-op.

You need to change the array element itself to refer to another string. Something like that:

newName[newName.Length - 1] = handover;
+9
source

Source: https://habr.com/ru/post/1763037/


All Articles