Home >Backend Development >C++ >Why Doesn't `string.Replace` Modify the Original String in .NET?
.NET string operation detailed explanation: Why
cannot modify the original string? string.Replace
Replace
In the following code fragment,
string.Replace
<code class="language-csharp">string someTestString = "<a href=\"myfoldert/108716305-1.jpg\" target=\"_blank\">108716305-1.jpg</a>"; someTestString.Replace("108716305", "NewId42");</code>
Explanation The string in
.NET is immutable, which means that their content cannot be directly modified. When calling , it creates a new string containing the replacement value, rather than modifying the original string. To update the string, you need: string.Replace
string.Replace
<code class="language-csharp">var newString = someTestString.Replace(someID.ToString(), sessionID);</code>It should be noted that this is suitable for all string operating functions in .NET, such as
<code class="language-csharp">someTestString = someTestString.Replace(someID.ToString(), sessionID);</code>
The above is the detailed content of Why Doesn't `string.Replace` Modify the Original String in .NET?. For more information, please follow other related articles on the PHP Chinese website!