Mohanapriya R Mohanapriya R
Updated date Apr 08, 2024
In this article, we will learn how to convert string to URL encode in C#.
  • 3.4k
  • 0
  • 0

Convert String to URL Encode in C#

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. For instance, a space (' ') becomes "%20," and an exclamation mark ('!') becomes "%21". This process ensures that the URL remains valid and free from any misinterpretations.

C# Program:

using System;
using System.Web;

class Program
{
    static void Main()
    {
        // Input string
        string inputString = "Hello, World! How are you?";

        // URL encode the string
        string urlEncodedString = HttpUtility.UrlEncode(inputString);

        // Output the results
        Console.WriteLine("Original String: " + inputString);
        Console.WriteLine("URL Encoded String: " + urlEncodedString);
    }
}

Running the Program:

After running the program, the output will demonstrate the conversion of the input string to its URL-encoded equivalent.

Output:

Original String: Hello, World! How are you?
URL Encoded String: Hello%2C+World%21+How+are+you%3F

Comments (0)

There are no comments. Be the first to comment!!!