Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何使用 .NET(VB 或 C#)轻松地将字符串编码为 utf8?例如,我需要编码一个字符串,如“This (is) my string”,结果应该是一个字符串“This+%28is%29+my+string”。
TIA
J.P
这是 URL 编码,而不是 UTF8 编码。试试这个方法:
HttpUtility.UrlEncode(value)
看起来您需要做的是 URL 编码,而不是“编码为 UTF-8”。
为此,使用
string encodedString = System.Web.HttpUtility.UrlEncode(yourString)
(UTF-8 是一种在内部表示 Unicode 字符的机制,而不是用于替换在 URI 中使用时有意义的字符。)