Convert space to "+" using C #

I want to convert a string to url, and instead of a space, it needs a “+” between keywords.

For instance:

"Hello I am"

at

"Hello+I+am"

How should I do it?

+3
source share
7 answers
String input = "Hello I am";
string output = input.Replace(" ", "+");
+3
source

For URLs, I highly recommend using Server.UrlEncode (in ASP.NET) or Uri.EscapeUriString (everywhere) instead of String.Replace.

+11
source

string.Replace:

"Hello I am".Replace(' ', '+');

, url ( ), Uri.EscapeUriString:

Uri.EscapeUriString("Hello I am");

MSDN:

EscapeUriString , RFC 2396, . (IRI) (IDN), EscapeUriString , RFC 3986, . UTF-8, .

+3

String.Replace

"Hello I am".Replace(' ','+');

+3

, , URL-, Replace:

string withSpaces = "Hello I am";

string withPluses = withSpaces.Replace(' ', '+');
+2
string s = "Hello I am";
s = s.Replace(" ", "+");
0

To answer the “convert string to url” part of the question (you don't have to manually convert the string if you want the correct URL):

string url = "http://www.baseUrl.com/search?q=" + HttpUtility.UrlEncode("Hello I am");

You call Url Encode for each parameter to encode the values ​​correctly.

0
source

Source: https://habr.com/ru/post/1774907/


All Articles