我要将URL A
解码为B
A) http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123
B)
http://example.com/xyz?params=id,expire&abc=123
这是一个示例网址,我寻找的是一般的解决方案,而不是A.Replace("\/", "/")...
目前我使用HttpUtility.UrlDecode(A, Encoding.UTF8)
和其他Encodings
,但不能生成URL B
!
发布于 2013-06-23 19:27:50
你只需要这个函数
System.Text.RegularExpressions.Regex.Unescape(str);
发布于 2011-08-09 09:35:12
这是我能想出的一个基本例子:
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();
}
这可能会遗漏很多东西,这取决于你将要得到的URL的类型。它不处理错误检查、文字转义,就像\\u0026
应该是\u0026
一样。我建议写几个单元测试来解决这个问题,并提供不同的输入。
https://stackoverflow.com/questions/6990347
复制相似问题