C# replace string in string

后端 未结 7 1211
情歌与酒
情歌与酒 2020-12-09 10:57

Is it possible to replace a substring in a string without assigning a return value?

I have a string:

string test = "Hello [REPLACE] world";
<         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 11:05

    Strings in .NET are immutable. They cannot be edited in-line.

    The closest you can get to in-line editing is to create a StringBuilder from a string. In-line fiddles with its contents and then get it to spit a string back out again.

    But this will still produce a new string rather than altering the original. It is a useful technique, though, to avoid generating lots of intermediary strings when doing lots of string fiddling, e.g. in a loop.

提交回复
热议问题