50

Are the any functions in C# that handle escape/unescape like JavaScript?

I have a JSON string like this:

{"Feeds":[{"Url":"www.test.com","FeedType":"Twitter"},{"Url":"www.test2.com","FeedType":"Youtube"}]}

Which looks like this after escape()

%7B%22Feeds%22%3A%5B%7B%22Url%22%3A%22www.test.com%22%2C%22FeedType%22%3A%22Twitter%22%7D%2C%7B%22Url%22%3A%22www.test2.com%22%2C%22FeedType%22%3A%22Youtube%22%7D%5D%7D

In my C# code I would like to unescape this string so that it looks exactly the same as it did before the escape()

Is this possible?

Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89

7 Answers7

74

HttpUtility.UrlDecode should do the trick.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • @Timwi - You are correct. I mis-read. Updated. Just out of curiosity, why did you roll your own code (now deleted) to do the same thing instead of re-using what .NET provides? – Justin Niessner Sep 23 '10 at 12:12
  • Thx, I tried `HttpUtility.HtmlDecode` but it did not work, but when I see your answer I see that `UrlDecode` does something that `HtmlDecode` did not do. – Martin at Mennt Sep 23 '10 at 12:19
  • @Martin - Yeah. `HtmlDecode` is for decoding escaped HTML characters. URL Encoding involves more rules. – Justin Niessner Sep 23 '10 at 12:21
  • Well firstly because I didn’t know about it at the time. Secondly because `HttpUtility` requires a reference to `System.Web.dll`, which is filled to the rim with stuff I have no use for. But thirdly because I enjoy rolling my own... I rolled my own webserver too... :-p – Timwi Sep 23 '10 at 16:23
  • 2
    This doesn't handle the '+' char. (Try using UrlDecode on "abc+def" it seems to be replacing the + with a space.) Temporarily I am using Microsoft.JScript.GlobalObject.unencode() But I hate bringing in a dependency just for that... – M Thelen Apr 18 '12 at 17:49
  • 3
    "+" is a space character in a URL. It's up to the encoder to escape that as well. In JavaScript you want to use encodeURIComponent instead of escape, which also covers the + character. – Neil Mar 05 '13 at 20:46
  • 12
    This is not decoding swedish characters properly- å gets escaped to %E5 and decoded to: � – Perbert May 07 '13 at 14:43
  • Downvoted, the second answer worked for "Lucía", this one didn't – achecopar Oct 06 '22 at 19:38
22

escape() is equivalent to

HttpUtility.UrlDecode(str, System.Text.Encoding.Default);

By default the UrlDecode uses UTF8 while escape() don't.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • UTF8 is backward compatible with ASCII so, unless I'm mis-understanding something, you shouldn't have to pass in the encoding. – Justin Niessner Sep 23 '10 at 12:15
  • 1
    This is wrong... escape() is actually equivalent to `HttpUtility.UrlDecode(str, Encoding.Default)`, which means two things: ① it depends on your system locale settings; and ② it is obsolete 1990s 8-bit technology and incompatible with UTF-8. (The second paragraph is correct though.) – Timwi Sep 23 '10 at 16:29
  • @Justin Niessner: As you said, it is backward `compatible`. Not equivalent. And I want to give emphasis on `backward`, don't even think it's compatible the other way. – BrunoLM Sep 23 '10 at 16:47
  • HttpUtility.UrlDecode(msg, System.Text.Encoding.Default); Works for me actually – Usman Younas Jul 18 '13 at 15:33
  • for languages with special chars this did the trick, thank you sir – Luis Miguel Apr 04 '17 at 10:41
11

This is the best way I found to work with these:

Encode in C#:

System.Uri.EscapeDataString("<string>");

Decode in JavaScript:

decodeURI("<string>");

Encode in JavaScript:

encodeURI("<string>");

Decode in C#:

System.Uri.UnescapeDataString("<string>");

Update 27-Jan-2016: Just found what seems do be a more compatible way to do it, which also encodes the URI protocol (http://) using javascript:

Encode in JavaScript:

encodeURIComponent("<string>");

Decode in JavaScript:

decodeURIComponent("<string>");
Vinicius
  • 1,601
  • 19
  • 19
5

Aw man, why do we over-think stuff so much sometimes. When an API function is being silly, send a karma cuss at the library developer, then work-around it...

HttpUtility.UrlEncode(editext, System.Text.Encoding.Default).Replace("+","%20");
Awerealis
  • 602
  • 6
  • 9
2
    internal static string UnJavascriptEscape(string s)
    {
        // undo the effects of JavaScript's escape function
        return HttpUtility.UrlDecode(s.Replace("+", "%2b"), Encoding.Default);
    }
1

To unescape without having to reference System.Web in order to use HttpUtility, try this:

Str = Str.Replace("+", " ");
Str = Regex.Replace(Str, "%([A-Fa-f\\d]{2})", a => "" + Convert.ToChar(Convert.ToInt32(a.Groups[1].Value, 16)));

Also, when I tried HttpUtility.UrlDecode, it didn't work for special characters áéíóúñ.

0

I spent 8 hours trying to get

HttpUtility.UrlDecode 

to work, and gave up and used

HttpUtility.HtmlDecode

which worked instantly.

MunsterMan
  • 307
  • 1
  • 8