String.Replace does not seem to replace brackets with empty string

后端 未结 9 634
鱼传尺愫
鱼传尺愫 2021-01-19 01:26

The following bit of C# code does not seem to do anything:

String str = \"{3}\";
str.Replace(\"{\", String.Empty);
str.Replace(\"}\", String.Empty);

Console         


        
9条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 01:54

    I guess you'll have to do

    String str = "{3}";
    str = str.Replace("{", String.Empty);
    str = str.Replace("}", String.Empty);
    
    Console.WriteLine(str);
    

    Look at the String.Replace reference:

    Return Value Type: System.String

    A String equivalent to this instance but with all instances of oldValue replaced with newValue.

提交回复
热议问题