The following bit of C# code does not seem to do anything:
String str = \"{3}\";
str.Replace(\"{\", String.Empty);
str.Replace(\"}\", String.Empty);
Console
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.
Str.Replace returns a new string. So, you need to use it as follows:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
The String class is immutable; str.Replace
will not alter str
, it will return a new string with the result. Try this one instead:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
String is immutable; you can't change an instance of a string. Your two Replace() calls do nothing to the original string; they return a modified string. You want this instead:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
It works this way in Java as well.
Replace actually does not modify the string instance on which you call it. It just returns a modified copy instead.
Try this one:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
besides all of the suggestions so far - you could also accomplish this without changing the value of the original string by using the replace functions inline in the output...
String str = "{3}";
Console.WriteLine(str.Replace("{", String.Empty).Replace("}", String.Empty));