How to decode "\ u0026" in URL?

I want to decode URL A to B :

A) http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123

B) http://example.com/xyz?params=id,expire&abc=123

This is an example URL, and I'm looking for a general solution, not A.Replace("\/", "/")...

I am currently using HttpUtility.UrlDecode(A, Encoding.UTF8) and other Encodings , but cannot create URL B !

+7
source share
2 answers

You only need this feature.

 System.Text.RegularExpressions.Regex.Unescape(str); 
+9
source

This is a basic example that I could come up with:

 static void Sample() { var str = @"http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123"; str = str.Replace("\\/", "/"); str = HttpUtility.UrlDecode(str); str = Regex.Replace(str, @"\\u(?<code>\d{4})", CharMatch); Console.Out.WriteLine("value = {0}", str); } private static string CharMatch(Match match) { var code = match.Groups["code"].Value; int value = Convert.ToInt32(code, 16); return ((char) value).ToString(); } 

This is probably not much, depending on the types of URLs you are going to get. It does not handle error checking, avoiding literals, for example \\u0026 should be \u0026 . I would recommend writing a few unit tests around this with various inputs to get started.

+2
source

All Articles