Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String Array Replace Last Element

i have a String Array which comes from a splitted String

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

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

Why doesnt this replaces my last element in the Array?

last() comes from using linq

regards

like image 503
Mark Avatar asked Dec 04 '22 11:12

Mark


1 Answers

Calling string.Replace doesn't alter the existing string - strings are immutable.

Instead, it returns a new string, with the appropriate replacements. However, you're not using the return value, so it's basically a no-op.

You need to change the array element itself to refer to a different string. Something like this:

newName[newName.Length - 1] = handover;
like image 200
Jon Skeet Avatar answered Dec 06 '22 02:12

Jon Skeet