C# program to replace all spaces in a string with ‘%20’

CsharpServer Side ProgrammingProgramming

We have a sample string with spaces −

str ="Hello World !";

Use the Replace() method in C# to replace all spaces in a string with ‘%20’ −

str2 = str.Replace(" ", "%20");

Example

You can try to run the following code to replace all spaces in a string with ‘%20’.

Live Demo

using System;
class Demo {
   static void Main() {
      String str, str2;
      str ="Hello World !";
      Console.WriteLine("String: "+str);
      str2 = str.Replace(" ", "%20");
      Console.WriteLine("String (After replacing): "+str2);
   }
}

Output

String: Hello World !
String (After replacing): Hello%20World%20!
raja
Published on 25-Jul-2018 11:20:21
Advertisements