C# program to replace all spaces in a string with %20
Here's a C# program that replaces all spaces in a string with "%20":
using System;
class Program
{
static void Main()
{
string inputString = "Hello, World!";
string replacedString = ReplaceSpaces(inputString);
Console.WriteLine("String after replacing spaces: " + replacedString);
}
static string ReplaceSpaces(string inputString)
{
// Replace spaces with "%20"
string replacedString = inputString.Replace(" ", "%20");
return replacedString;
}
}
In this program, we have the 'ReplaceSpaces' method that takes a string as input and returns a new string where all spaces have been replaced with "%20".
We use the 'Replace' method of the 'string' class to replace all occurrences of the space character (" ") with "%20". The 'Replace' method returns a new string with the replacements made.
Finally, we return the replaced string.
In the example above, the output would be:
String after replacing spaces: Hello,%20World!
The spaces in the input string "Hello, World!" have been replaced with "%20".