与 javascript 的 unescape() 等效的 c# 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6833865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:01:39  来源:igfitidea点击:

what is the c# equivalent to javascript's unescape()?

javascriptc#.net

提问by vivek a.

I am trying to analyse some JavaScript, and one line is

我正在尝试分析一些JavaScript,其中一行是

var x = unescape("%u4141%u4141 ......"); 

with lots of characters in form %uxxxx.

形式上有很多字符%uxxxx

I want to rewrite the JavaScriptin c#but can't figure out the proper function to decode a string of characters like this. I've tried

我想重写JavaScriptinc#但无法找出正确的函数来解码这样的字符串。我试过了

HttpUtility.HTMLDecode("%u4141%u4141");

but this did not change these characters at all.

但这根本没有改变这些字符。

How can I accomplish this in c#?

我怎样才能在 c# 中完成这个?

回答by dlev

You can use UrlDecode:

您可以使用UrlDecode

string decoded = HttpUtility.UrlDecode("%u4141%u4141");

decodedwould then contain "??".

decoded然后将包含"??".

As other have pointed out, changing the %to \would work, but UrlDecodeis the preferred method, since that ensures that other escaped symbols are translated correctly as well.

正如其他人指出的那样,更改%to\会起作用,但UrlDecode它是首选方法,因为这可确保其他转义符号也能正确翻译。

回答by andynormancx

You need HttpUtility.UrlDecode. You shouldn't really be using escape/unescapein most cases nowadays, you should be using things like encodeURI/decodeURI/encodeURIComponent.

你需要HttpUtility.UrlDecode. escape/unescape现在大多数情况下你不应该真的使用,你应该使用像encodeURI/decodeURI/encodeURIComponent.

When are you supposed to use escape instead of encodeURI / encodeURIComponent?

你什么时候应该使用escape而不是encodeURI / encodeURIComponent?

This questioncovers the issue of why escape/unescapeare a bad idea.

这个问题涵盖了为什么escape/unescape是一个坏主意的问题。

回答by hs.jalilian

You can call bellow method to achieve the same effect as Javascript escape/unescape method

可以调用 bellow 方法来达到与 Javascript 转义/转义方法相同的效果

Microsoft.JScript.GlobalObject.unescape();

Microsoft.JScript.GlobalObject.escape();

回答by dougstefe

edit the web.config the following parameter:

编辑 web.config 以下参数:

< globalization requestEncoding="iso-8859-15" responseEncoding="utf-8" >responseHeaderEncoding="utf-8"in < system.web >

< globalization requestEncoding="iso-8859-15" responseEncoding="utf-8" >responseHeaderEncoding="utf-8"< system.web >

回答by Brian Gordon

Change the % signs to backslashes and you have a C# string literal. C# treats \uxxxx as an escape sequence, with xxxx being 4 digits.

将 % 符号更改为反斜杠,您就有了一个 C# 字符串文字。C# 将 \uxxxx 视为转义序列,其中 xxxx 为 4 位数字。

回答by DrAlligieri

In basic string usage you can initiate string variable in Unicode: var someLine="\u4141"; If it is possible - replace all "%u" with "\u".

在基本的字符串用法中,您可以在 Unicode 中启动字符串变量: var someLine="\u4141"; 如果可能 - 用“\u”替换所有的“%u”。