简体   繁体   中英

Replace ASCII characters with their equivalent

I am setting a value in the cookie using JavaScript and getting the contents of the cookie in the code behind.

But the problem is if I am storing the string with some special characters or whitespace characters, when I am retrieving the contents of the cookie the special symbols are getting converted into ASCII equivalent.

For example, if I want to store Adam - (SET) in cookie , its getting converted into Adam%20-%20%28SET%29 and getting stored and when I am retrieving it I get the same Adam%20-%20%28SET%29 . But I wan tot get this Adam - (SET) in the code behind.

How I get this. Please help.

In C#

Use:

String decoded = HttpUtility.UrlDecode(EncodedString);

HttpUtility.UrlDecode() is the underlying function used by most of the other alternatives you can use in the .NET Framwework (see below).

You may want to specify an encoding, if necessary.

Or:

String decoded = Uri.UnescapeDataString(s);

See Uri.UnescapeDataString() 's documentation for some caveats.

In JavaScript

var decoded = decodeURIComponent(s);

Before jumping on using unescape as recommended in other questions, read decodeURIComponent vs unescape, what is wrong with unescape? . You may also want to read What is the difference between decodeURIComponent and decodeURI? .

您可以使用JS中的unescape函数来执行此操作。

var str = unescape('Adam%20-%20%28SET%29');

您正在寻找HttpUtility.UrlDecode() (我认为是在System.Web名称空间中)

在javasrcipt你可以使用内置的decodeURIComponent ,但我怀疑,当所以C#的答案是你想要的值发送到服务器的字符串编码发生。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM