C# program to replace all spaces in a string with β€˜%20’


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!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 19-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements