与 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
what is the c# equivalent to javascript's unescape()?
提问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 JavaScript
in c#
but can't figure out the proper function to decode a string of characters like this. I've tried
我想重写JavaScript
inc#
但无法找出正确的函数来解码这样的字符串。我试过了
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");
decoded
would then contain "??"
.
decoded
然后将包含"??"
.
As other have pointed out, changing the %
to \
would work, but UrlDecode
is 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/unescape
in 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/unescape
are 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”。