encode string to utf8

2019-08-29 09:45发布

How can easily encode a string to utf8 using .NET (VB or C#)? For instance I need to encode a string like "This (is) my string" the result should be a string "This+%28is%29+my+string".

TIA

JP

2条回答
贪生不怕死
2楼-- · 2019-08-29 10:35

This is URL encoding, not UTF8 encoding. Try this method:

HttpUtility.UrlEncode(value)
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-29 10:39

It looks like what you need to do is URL encoding, not "encoding to UTF-8".

For that, use

string encodedString = System.Web.HttpUtility.UrlEncode(yourString)

(UTF-8 is a mechanism for representing Unicode characters internally, not for replacing characters that otherwise have meaning when used in a URI.)

查看更多
登录 后发表回答