Sai A Sai A
Updated date May 08, 2023
In this blog, we explored several methods for converting URL-encoded strings to normal strings in C#. The methods include using the Uri class, HttpUtility class, Regex class, Encoding class, and the Uri.TryCreate() method.
  • 1.7k
  • 0
  • 0

Introduction:

URL encoding is a method used to encode characters in a URL to ensure that the URL remains valid and does not get distorted during transmission. URL encoding replaces special characters with a percentage sign (%) followed by two hexadecimal digits. The hexadecimal value represents the character code of the original character. In C#, there are different ways to convert URL-encoded strings to normal strings. In this blog post, we will explore several methods for doing this.

Method 1: Using the Uri Class

The Uri class in C# provides a built-in method for decoding URL-encoded strings. The method is called Uri.UnescapeDataString(). Here is an example of how to use it:

string urlEncodedString = "Hello%20World%21";
string decodedString = Uri.UnescapeDataString(urlEncodedString);
Console.WriteLine(decodedString);

Output:

Hello World!

In the example above, we created a URL-encoded string "Hello%20World%21". We then used the Uri.UnescapeDataString() method to decode the string. The method returned the decoded string "Hello World!".

Method 2: Using HttpUtility.UrlDecode()

The HttpUtility class in C# provides a method for decoding URL-encoded strings. The method is called HttpUtility.UrlDecode(). Here is an example of how to use it:

string urlEncodedString = "Hello%20World%21";
string decodedString = HttpUtility.UrlDecode(urlEncodedString);
Console.WriteLine(decodedString);

Output:

Hello World!

In the example above, we created a URL encoded string "Hello%20World%21". We then used the HttpUtility.UrlDecode() method to decode the string. The method returned the decoded string "Hello World!".

Method 3: Using Regex.Unescape()

The Regex class in C# provides a method for unescaping a string that has been escaped using backslashes. We can use this method to decode URL-encoded strings as well. Here is an example of how to use it:

string urlEncodedString = "Hello%20World%21";
string decodedString = Regex.Unescape(Uri.UnescapeDataString(urlEncodedString));
Console.WriteLine(decodedString);

Output:

Hello World!

In the example above, we created a URL-encoded string "Hello%20World%21". We first used the Uri.UnescapeDataString() method to decode the string, and then used the Regex.Unescape() method to unescape the string. The method returned the decoded string "Hello World!".

Method 4: Using Encoding.UTF8.GetString()

We can also use the Encoding.UTF8.GetString() method to decode URL-encoded strings. Here is an example of how to use it:

string urlEncodedString = "Hello%20World%21";
string decodedString = Encoding.UTF8.GetString(HttpUtility.UrlDecodeToBytes(urlEncodedString));
Console.WriteLine(decodedString);

Output:

Hello World!

In the example above, we created a URL-encoded string "Hello%20World%21". We first used the HttpUtility.UrlDecodeToBytes() method to decode the string to bytes, and then used the Encoding.UTF8.GetString() method to convert the bytes to a string. The method returned the decoded string "Hello World!".

Method 5: Using Uri.TryCreate()

The Uri.TryCreate() method can also be used to decode URL-encoded strings. Here is an example of how to use it:

string urlEncodedString = "Hello%20World%21";
Uri.TryCreate(urlEncodedString, UriKind.RelativeOrAbsolute, out Uri uriResult);
string decodedString = uriResult.ToString();
Console.WriteLine(decodedString);

Output:

Hello World!

In the example above, we created a URL-encoded string "Hello%20World%21". We then used the Uri.TryCreate() method to create a Uri object from the encoded string. Finally, we used the ToString() method to get the decoded string "Hello World!".

Conclusion:

This blog post explored several methods for converting URL-encoded strings to normal strings in C#. We used the Uri class, HttpUtility class, Regex class, Encoding class, and the Uri.TryCreate() method to decode URL-encoded strings. All of these methods are easy to use and provide accurate results.

Comments (0)

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