Introduction:
In web applications, it is quite common to deal with URL-encoded strings. These strings are encoded in a way to make them compatible with the standard ASCII character set. The reason behind URL encoding is to provide a consistent mechanism for transmitting data between different systems. In C#, we can decode URL-encoded strings using different methods. In this blog, we will discuss different methods to convert a URL-encoded string to its original form in C#.
Method 1: Using HttpUtility.UrlDecode Method
One of the easiest ways to convert a URL-encoded string to its original form is by using the HttpUtility.UrlDecode
method. This method is available in the System.Web namespace, so make sure to include this namespace in your project.
Here is a sample program that demonstrates the usage of the HttpUtility.UrlDecode
method:
using System;
using System.Web;
class Program
{
static void Main()
{
string encodedString = "Hello%20World%21";
string decodedString = HttpUtility.UrlDecode(encodedString);
Console.WriteLine(decodedString);
}
}
Output:
Hello World!
In the above program, we have declared an encoded string "Hello%20World%21". This string contains the URL encoded form of "Hello World!" where %20 is used to represent a space and %21 is used to represent an exclamation mark.
Then we have used the HttpUtility.UrlDecode
method to decode the encoded string. This method takes an encoded string as input and returns the decoded string as output. In this case, the decoded string is "Hello World!".
Method 2: Using Uri.UnescapeDataString Method
Another method to decode a URL-encoded string is by using the Uri.UnescapeDataString
method. This method is available in the System namespace, so you don't need to import any additional namespaces.
Here is a sample program that demonstrates the usage of the Uri.UnescapeDataString
method:
using System;
class Program
{
static void Main()
{
string encodedString = "Hello%20World%21";
string decodedString = Uri.UnescapeDataString(encodedString);
Console.WriteLine(decodedString);
}
}
Output:
Hello World!
In the above program, we have used the Uri.UnescapeDataString
method to decode the encoded string "Hello%20World%21". This method takes an encoded string as input and returns the decoded string as output. In this case, the decoded string is "Hello World!".
Method 3: Using Regular Expressions
We can also use regular expressions to decode a URL-encoded string. This method involves writing a regular expression pattern to match the encoded characters and replace them with their original form.
Here is a sample program that demonstrates the usage of regular expressions to decode a URL-encoded string:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string encodedString = "Hello%20World%21";
string decodedString = Regex.Replace(encodedString, "(%[0-9a-fA-F][0-9a-fA-F])+", match => ((char)Convert.ToInt32(match.Value.Substring(1), 16)).ToString());
Console.WriteLine(decodedString);
}
}
Output:
Hello World!
In the above program, we have used the Regex.Replace
method to decode the encoded string "Hello%20World%21". This method takes a regular expression pattern as input, which matches the encoded characters in the string.
The regular expression pattern used in this program is "(%[0-9a-fA-F][0-9a-fA-F])+". This pattern matches all occurrences of the encoded characters in the string. The pattern "%[0-9a-fA-F][0-9a-fA-F]" matches any two characters after the % symbol that represents the encoded character.
The match => ((char)Convert.ToInt32(match.Value.Substring(1), 16)).ToString()
is the lambda expression used to replace the encoded characters with their original form. This expression takes the matched string as input and returns the corresponding decoded character.
Method 4: Using Encoding.UTF8.GetString Method
We can also use the Encoding.UTF8.GetString
method to decode a URL-encoded string. This method takes a byte array as input, which is obtained by converting the encoded string to bytes using the Encoding.UTF8.GetBytes
method.
Here is a sample program that demonstrates the usage of the Encoding.UTF8.GetString
method to decode a URL-encoded string:
using System;
using System.Text;
class Program
{
static void Main()
{
string encodedString = "Hello%20World%21";
byte[] bytes = Encoding.UTF8.GetBytes(encodedString.Replace("+", " "));
string decodedString = Encoding.UTF8.GetString(bytes);
Console.WriteLine(decodedString);
}
}
Output:
Hello World!
In the above program, we have used the Encoding.UTF8.GetString
method to decode the encoded string "Hello%20World%21". This method takes a byte array as input, which is obtained by converting the encoded string to bytes using the Encoding.UTF8.GetBytes
method.
We have also replaced the "+" symbol with a space character because the Encoding.UTF8.GetBytes
method does not recognize the "+" symbol as a space character.
Conclusion:
In this blog, we have discussed different methods to decode a URL-encoded string in C#. We have used the HttpUtility.UrlDecode
method, Uri.UnescapeDataString
method, regular expressions, and Encoding.UTF8.GetString
method to decode URL encoded strings. All of these methods are simple and easy to use, and you can choose any of them based on your requirements.
Comments (0)