Does C# have an equivalent to JavaScript's encodeURIComponent()?

Posted by travis on Stack Overflow See other posts from Stack Overflow or by travis
Published on 2008-09-17T19:05:10Z Indexed on 2010/12/29 4:54 UTC
Read the original article Hit count: 251

Filed under:
|
|
|
|

In JavaScript:

encodeURIComponent("©v") == "%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 "©v" 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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about JavaScript