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

后端 未结 9 630
鱼传尺愫
鱼传尺愫 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 02:16

    The Replace method returns a string with the replacement. What I think you're looking for is this:

    str = str.Replace("{", string.Empty);
    str = str.Replace("}", string.Empty);
    
    Console.WriteLine(str);
    
    0 讨论(0)
  • 2021-01-19 02:18

    I believe that str.Replace returns a value which you must assign to your variable. So you will need to do something like:

    String str = "{3}";
    str = str.Replace("{", String.Empty);
    str = str.Replace("}", String.Empty);
    
    Console.WriteLine(str);
    
    0 讨论(0)
  • 2021-01-19 02:19

    The Replace function returns the modified string, so you have to assign it back to your str variable.

    String str = "{3}";
    str = str.Replace("{", String.Empty);
    str = str.Replace("}", String.Empty);
    
    Console.WriteLine(str);
    
    0 讨论(0)
提交回复
热议问题