I am trying to use the HttpUtility.HtmlEncode to encode an underscore to %5f, but the encoded value does not give me the hex representation. How will I accomplish this?
string encodedValue = HttpUtility.HtmlEncode("First_Name");
The value I want in the encodedValue string is "First%5FName".
Is there something I am missing? I have also tried using the HttpUtility.UrlEncode, but that does not give me the desired result as well.
I know this should be something simple, but I cant get around it.
What is URL encoding or Percent Encoding? URLs in the world wide web can only contain ASCII alphanumeric characters and some other safe characters like hyphen ( - ), underscore ( _ ), tilde ( ~ ), and dot ( . ). Alphabets / Digits / "-" / "_" / "~" / "." Any other character apart from the above list must be encoded.
A URL is composed from a limited set of characters belonging to the US-ASCII character set. These characters include digits (0-9), letters(A-Z, a-z), and a few special characters ( "-" , "." , "_" , "~" ).
if you just want to replace _
with %5f
, you can just use myString.Replace("_", "%5f");
It's an old subject but to convert a char in its HexEscape format you should use:
myString.Replace("_", Uri.HexEscape("_"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With