Convert Strings to URL Decode using C#
URL encoding is a mechanism to replace special characters with percent-encoded equivalents. For example, a space (' ') in a URL becomes '%20', and an ampersand ('&') becomes '%26'. This encoding ensures that URLs remain valid and can be safely transmitted over the internet.
Why URL Decoding:?
While working with web applications or handling data from URLs, it's common to encounter URL-encoded strings. To process or display this data correctly, it becomes essential to decode these strings back to their original form. C# provides a easy method for this task - HttpUtility.UrlDecode
.
using System;
using System.Web;
class Program
{
static void Main()
{
// URL-encoded string
string encodedString = "Hello%20World%21";
// Decoding the URL-encoded string
string decodedString = HttpUtility.UrlDecode(encodedString);
// Displaying the original and decoded strings
Console.WriteLine("Original String: " + encodedString);
Console.WriteLine("Decoded String : " + decodedString);
}
}
Output:
Original String: Hello%20World%21
Decoded String : Hello World!
- We start by defining a URL-encoded string (
encodedString
). - The
HttpUtility.UrlDecode
method is then used to decode the string, and the result is stored indecodedString
. - Finally, the original and decoded strings are displayed, showcasing the transformation.
Comments (0)