Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace all the spaces with %20 in C#?

Tags:

c#

url

urlencode

I want to make a string into a URL using C#. There must be something in the .NET framework that should help, right?

like image 512
Haim Bender Avatar asked Oct 04 '09 22:10

Haim Bender


People also ask

How do you replace all spaces in a string?

Use the String. replace() method to replace all spaces in a string, e.g. str. replace(/ /g, '+'); . The replace() method will return a new string with all spaces replaced by the provided replacement.

How do you replace %20 with space?

There are numerous ways to replace %20 with space. We are going to use the simplest approach which involves the usage of the decodeURIComponent() and trim() methods. The trim() method will remove any extra space at the beginning as well as at the end of the string.

Is space special character in C?

When parsing code, the C compiler ignores white-space characters unless you use them as separators or as components of character constants or string literals. Use white-space characters to make a program more readable. Note that the compiler also treats comments as white space.

How do you spacing in C programming?

For just a space, use ' ' .


2 Answers

Another way of doing this is using Uri.EscapeUriString(stringToEscape).

like image 171
Dirk Vollmar Avatar answered Oct 20 '22 10:10

Dirk Vollmar


I believe you're looking for HttpServerUtility.UrlEncode.

System.Web.HttpUtility.UrlEncode(string url) 
like image 37
LiraNuna Avatar answered Oct 20 '22 08:10

LiraNuna