3

I have an issue when converting a string from stringbuilder to string. The issue is similar to this issue but slightly different:

This is my code simplified:

StringBuilder sb = new StringBuilder();
sb.Append("\"");
sb.Append("Hello World");
sb.Append("\"");

string test = sb.ToString();

Now in the debugger the sb value is:

"Hello World"

In the debugger the test string value is changed to:

\"Hello World\"

When returning the test string value back to the browser the velue is STILL escaped:

\"Hello World\"

I have tried using the string replace:

test = test.Replace("\"", "");

no luck, I tried appending the ASCII character instead of \" and I have also tried a different append

sb.Append('"');

All these with no luck. Can somebody maybe point me in the right direction of why I'm still getting the escape character and how to get rid of it.

Thanks and appreciate any input.

Community
  • 1
  • 1
user281921
  • 661
  • 2
  • 8
  • 25
  • see http://stackoverflow.com/questions/1928909/in-c-can-i-escape-a-double-quote-in-a-literal-string – hatchet - done with SOverflow Feb 11 '13 at 18:43
  • Hi, using the @"""" made no difference. When I used sb.Append(@""""); the resulting stings still contains the \" in it. – user281921 Feb 11 '13 at 19:05
  • I have also tested the output by opening the URL in IE and saving to disk. After opening the result in notepad the escape slashes are still there so it's 100% not a debugger or watch issue. – user281921 Feb 11 '13 at 19:13

4 Answers4

1

Ok it seems that in WCF the stringBuilder automatically adds escape quotes. This means you can not get away from that. Also I was going about this all wrong. I was trying to return a string where I was supposed to return a serialised JSON object.

user281921
  • 661
  • 2
  • 8
  • 25
0

I'm not seeing the behavior you describe. Escaping double quotes with the backslash should work. The following snippet of code

        var sb = new StringBuilder();
        sb.Append("Ed says, ");
        sb.Append("\"");
        sb.Append("Hello");
        sb.Append("\"");
        Console.WriteLine(sb.ToString());
        foreach (char c in sb.ToString()) Console.Write(c + "-");
        Console.ReadKey();

produces

Ed says, "Hello"
E-d- -s-a-y-s-,- -"-H-e-l-l-o-"-

If you are getting actual backslash characters in your final display of the string, that may be getting added by something after the StringBuilder and ToString code.

  • Ok I can't do a ConsoleWrite as it's web service but I have done a StreamWriter to a local file. The output is fine, no escape characters. Which begs the question why does the data displayed in the browser window contain escape characters. – user281921 Feb 11 '13 at 19:38
  • @Pete I'd have to see how you're outputting the string into your HTML. In general though, a double quote must be encoded as `"`. Possibly the problem is that you're not encoding your string using something like Html.Encode(). In that case, you should be building your string with `"` instead of the double quote character. – hatchet - done with SOverflow Feb 11 '13 at 19:45
  • tried the " but that translates directly to that string. html.encode still produces the escape character – user281921 Feb 13 '13 at 15:52
  • Ok it seems that in WCF the stringBuilder automatically adds escape quotes. This means you can not get away from that. Also I was going about this all wrong. I was trying to return a string where I was supposed to return a serialised JSON object. – user281921 Feb 19 '13 at 10:59
0

You can use a verbatim string literal "@" before the string, then enter the quotes twice. This removes the use to use escapes in the string sequence :)

StringBuilder sb = new StringBuilder();
sb.Append(@"""");
sb.Append("Hello World");
sb.Append(@"""");

string test = sb.ToString();
NewZeroRiot
  • 552
  • 1
  • 5
  • 22
0

This question and answer thread kept on coming up when searching for the solution. The confusion, for me, was that the Debugger escaping looks exactly the same as the JSON serializer behaviour that was being applied later when I returned the string to a client. So the code at the top of the thread (and my code) worked correctly.

Once I realised that, I converted the piece of code I was working on return an array (string[] in this case) and store that rather than the original string object. Later the JSONResult serializer then dealt with converting the array correctly.

cigien
  • 57,834
  • 11
  • 73
  • 112
From Orbonia
  • 616
  • 5
  • 16