encode string to utf8

烈酒焚心 提交于 2019-12-25 03:24:15

问题


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


回答1:


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

HttpUtility.UrlEncode(value)



回答2:


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.)



来源:https://stackoverflow.com/questions/1888066/encode-string-to-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!