且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将HTML编码的字符串转换为普通字符串?

更新时间:2023-02-03 10:12:46

如果你使用.NET 4.5:

If you use .NET 4.5:
using System.Net; // add this at the top of your code file
// the code to decode:
string input = "http%3A%3D%3Dgoogle%2Ecom";
string decoded = WebUtility.UrlDecode(input);



WebUtility类不存在在旧版本中,如果您使用较旧的.NET版本,请添加对System.Web的引用并使用此代码:


The WebUtility class doesn't exist in older versions, so if you use an older .NET version, add a reference to System.Web and use this code:

using System.Web; // add this at the top of your code file
// the code to decode:
string input = "http%3A%3D%3Dgoogle%2Ecom";
string decoded = HttpUtility.UrlDecode(input);



注意:上面的代码转换它进入 http:== google.com 而不是 http://google.com ,因为%3D 不代表斜线。如果你想要斜杠,请使用%2F


Note: the above code transforms it into http:==google.com and not http://google.com, because %3D doesn't represent a slash. Use %2F if you want a slash.