admin管理员组文章数量:1134232
In JavaScript:
encodeURIComponent("©√") == "%C2%A9%E2%88%9A"
Is there an equivalent for C# applications? For escaping HTML characters I used:
txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
m => @"&#" + ((int)m.Value[0]).ToString() + ";");
But I'm not sure how to convert the match to the correct hexadecimal format that JS uses. For example this code:
txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
m => @"%" + String.Format("{0:x}", ((int)m.Value[0])));
Returns "%a9%221a"
for "©√"
instead of "%C2%A9%E2%88%9A"
. It looks like I need to split the string up into bytes or something.
Edit: This is for a windows app, the only items available in System.Web
are: AspNetHostingPermission
, AspNetHostingPermissionAttribute
, and AspNetHostingPermissionLevel
.
In JavaScript:
encodeURIComponent("©√") == "%C2%A9%E2%88%9A"
Is there an equivalent for C# applications? For escaping HTML characters I used:
txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
m => @"&#" + ((int)m.Value[0]).ToString() + ";");
But I'm not sure how to convert the match to the correct hexadecimal format that JS uses. For example this code:
txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
m => @"%" + String.Format("{0:x}", ((int)m.Value[0])));
Returns "%a9%221a"
for "©√"
instead of "%C2%A9%E2%88%9A"
. It looks like I need to split the string up into bytes or something.
Edit: This is for a windows app, the only items available in System.Web
are: AspNetHostingPermission
, AspNetHostingPermissionAttribute
, and AspNetHostingPermissionLevel
.
7 Answers
Reset to default 290Uri.EscapeDataString
or HttpUtility.UrlEncode
is the correct way to escape a string meant to be part of a URL.
Take for example the string "Stack Overflow"
:
HttpUtility.UrlEncode("Stack Overflow")
-->"Stack+Overflow"
Uri.EscapeUriString("Stack Overflow")
-->"Stack%20Overflow"
Uri.EscapeDataString("Stack + Overflow")
--> Also encodes"+" to "%2b"
---->Stack%20%2B%20%20Overflow
Only the last is correct when used as an actual part of the URL (as opposed to the value of one of the query string parameters)
HttpUtility.HtmlEncode
/ Decode
HttpUtility.UrlEncode
/ Decode
You can add a reference to the System.Web
assembly if it's not available in your project
I tried to do full compatible analog of javascript's encodeURIComponent for c# and after my 4 hour experiments I found this
c# CODE:
string a = "!@#$%^&*()_+ some text here али мамедов баку";
a = System.Web.HttpUtility.UrlEncode(a);
a = a.Replace("+", "%20");
the result is: !%40%23%24%25%5e%26*()_%2b%20some%20text%20here%20%d0%b0%d0%bb%d0%b8%20%d0%bc%d0%b0%d0%bc%d0%b5%d0%b4%d0%be%d0%b2%20%d0%b1%d0%b0%d0%ba%d1%83
After you decode It with Javascript's decodeURLComponent();
you will get this: !@#$%^&*()_+ some text here али мамедов баку
Thank You for attention
System.Uri.EscapeUriString() didn't seem to do anything, but System.Uri.EscapeDataString() worked for me.
For a Windows Store App, you won't have HttpUtility. Instead, you have:
For an URI, before the '?':
- System.Uri.EscapeUriString("example.com/Stack Overflow++?")
- -> "example.com/Stack%20Overflow++?"
For an URI query name or value, after the '?':
- System.Uri.EscapeDataString("Stack Overflow++")
- -> "Stack%20Overflow%2B%2B"
For a x-www-form-urlencoded query name or value, in a POST content:
- System.Net.WebUtility.UrlEncode("Stack Overflow++")
- -> "Stack+Overflow%2B%2B"
Try Server.UrlEncode()
, or System.Web.HttpUtility.UrlEncode()
for instances when you don't have access to the Server
object. You can also use System.Uri.EscapeUriString()
to avoid adding a reference to the System.Web
assembly.
You can use the Server object in the System.Web namespace
Server.UrlEncode, Server.UrlDecode, Server.HtmlEncode, and Server.HtmlDecode.
Edit: poster added that this was a windows application and not a web one as one would believe. The items listed above would be available from the HttpUtility class inside System.Web which must be added as a reference to the project.
本文标签: Does C have an equivalent to JavaScript39s encodeURIComponent()Stack Overflow
版权声明:本文标题:Does C# have an equivalent to JavaScript's encodeURIComponent()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736770078a1952033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论