如何在 C# 中将所有空格替换为 %20?
我想使用 C# 将字符串变成 URL。 .NET 框架中一定有一些可以提供帮助的东西,对吗?
I want to make a string into a URL using C#. There must be something in the .NET framework that should help, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
另一种方法是使用
Uri.EscapeUriString(stringToEscape )
。Another way of doing this is using
Uri.EscapeUriString(stringToEscape)
.我相信您正在寻找 HttpServerUtility.UrlEncode。
I believe you're looking for HttpServerUtility.UrlEncode.
我发现有用的
System.Web.HttpUtility.UrlPathEncode(string str);
它用 %20 而不是 + 替换空格。
I found useful
System.Web.HttpUtility.UrlPathEncode(string str);
It replaces spaces with %20 and not with +.
要正确转义空格以及其余特殊字符,请使用
System.Uri.EscapeDataString(string stringToEscape)
。To properly escape spaces as well as the rest of the special characters, use
System.Uri.EscapeDataString(string stringToEscape)
.正如对已批准的故事的评论, HttpServerUtility.UrlEncode 方法替换了空格用 + 代替 %20。
请改用以下两种方法之一: Uri.EscapeUriString() 或 Uri.EscapeDataString()
示例代码:
As commented on the approved story, the HttpServerUtility.UrlEncode method replaces spaces with + instead of %20.
Use one of these two methods instead: Uri.EscapeUriString() or Uri.EscapeDataString()
Sample code:
我也需要这样做,几年前发现了这个问题,但问题标题和文本不太匹配,并使用
Uri.EscapeDataString
或UrlEncode
(不要请使用那个!)通常没有意义,除非我们正在讨论将 URL 作为参数传递给其他 URL。(例如,在进行开放 ID 身份验证、Azure AD 等时传递回调 URL)
希望这是对问题的更务实的回答:我想使用 C# 将字符串制作为 URL,其中必须有一些内容.NET 框架应该会有所帮助,对吧?
是 - 有两个函数有助于在 C#
String.Format
中制作 URL 字符串以格式化 URLUri.EscapeDataString
用于转义 URL 中的任何参数此代码
产生此结果
https://site/app/?q=search%20for%20cats&redirectUrl=https%3A%2F%2Fmysite%2Fmyapp
可以安全地复制并粘贴到浏览器的地址栏或 HTML
A
标记的src
属性中,或与curl
一起使用code>,或编码成QR码等。I needed to do this too, found this question from years ago but question title and text don't quite match up, and using
Uri.EscapeDataString
orUrlEncode
(don't use that one please!) doesn't usually make sense unless we are talking about passing URLs as parameters to other URLs.(For example, passing a callback URL when doing open ID authentication, Azure AD, etc.)
Hoping this is more pragmatic answer to the question: I want to make a string into a URL using C#, there must be something in the .NET framework that should help, right?
Yes - two functions are helpful for making URL strings in C#
String.Format
for formatting the URLUri.EscapeDataString
for escaping any parameters in the URLThis code
produces this result
https://site/app/?q=search%20for%20cats&redirectUrl=https%3A%2F%2Fmysite%2Fmyapp
Which can be safely copied and pasted into a browser's address bar, or the
src
attribute of a HTMLA
tag, or used withcurl
, or encoded into a QR code, etc.使用
HttpServerUtility.UrlEncode
< /a>Use
HttpServerUtility.UrlEncode
HttpUtility.UrlDecode 对我有用:
str2 =“name=John Doe”
HttpUtility.UrlDecode works for me:
str2 = "name=John Doe"
HttpUtility.UrlEncode 方法(字符串)
HttpUtility.UrlEncode Method (String)
以下代码将用单个 %20 字符替换重复空格。
示例:
输入为:
输出:
代码
The below code will replace repeating space with a single %20 character.
Example:
Input is:
Output:
Code
HttpServerUtility.HtmlEncode
来自文档:
但这实际上编码了 HTML,而不是网址。请改用UrlEncode(TestString)。
HttpServerUtility.HtmlEncode
From the documentation:
But this actually encodes HTML, not URLs. Instead use UrlEncode(TestString).