How to convert currency symbol to corresponding HTML entity

天大地大妈咪最大 提交于 2020-04-15 22:42:50

问题


System.Net.WebUtility.HtmlDecode("€"); // returns €
System.Net.WebUtility.HtmlEncode("€"); // also returns €

How can I convert € (or any other currency symbol) to corresponding html entity.

In this example € => €

I'm using .Net 4.6.1


回答1:


HtmlEncode only looks for a few special characters and replaces them with hard-coded values, and additionally a few more high ASCII characters (160 - 255) as described here. The only way to encode into entity names is by specifying them manually. I gave it a shot and built a wrapper around the System.Net.WebUtility class while leveraging the existing Html entities dataset used by .NET to decode as well so that decoding continues to work with this solution. I've hosted it on github: WebUtilityWrapper. You can use it as shown below:

WebUtilityWrapper.HtmlEncode("€"); // Returns €
WebUtilityWrapper.HtmlEncode("Δ"); // Returns Δ
WebUtilityWrapper.HtmlEncode("&"); // Returns &
WebUtilityWrapper.HtmlEncode("$"); // Returns $ 
WebUtilityWrapper.HtmlEncode("€¢£¥"); // Returns €¢£¥

I've tested it out by encoding and decoding back and then verifying if we get the original string for a large set of unicode characters. Sharing some more tests:

HtmlEncode() Response using framework's HtmlEncode: (link)

// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~

// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°
±´µ¶¸ºÀÁÂÃÄÅÆ
ÇÈÉÊËÌÍÎÏÐÑÒÓ
ÔÕÖ×ØÙÚÛÜÝÞßà
áâãäåæçèéêëìí
îïðñòóôõö÷øùú
ûüýþ

// Unicodes for Greek Alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝ

// Unicodes for 9824 - 9830
♠♣♥♦

HtmlEncode() Response using WebUtilityWrapper.HtmlEncode:

// Alphabets
$+0123456789<=>ABCDEFGHIJKLMNOPQRSTUVWXYZ^`abcdefghijklmnopqrstuvwxyz|~

// Unicode 162 to 254
¢£¤¥¦§¨©ª¬®¯°
±´µ¶¸ºÀÁÂÃÄÅÆ
ÇÈÉÊËÌÍÎÏÐÑÒÓ
ÔÕÖ×ØÙÚÛÜÝÞßà
áâãäåæçèéêëìí
îïðñòóôõö÷øùú
ûüýþ

// Unicodes for Greek alphabet
ΑΒΓΔΕΖΗΘΙΚΛΜΝ
ΞΟΠΡΣΤΥΦΧΨΩ

// Unicodes for 9824 - 9830
♠♣♥♦

Hope this helps!




回答2:


HtmlEncode and HtmlDecode are not symmetric.

HtmlEncode will only encode a handful of special characters:

  • < => &lt;
  • > => &gt;
  • & => &amp;
  • ' => &#39;
  • " => &quot;

and for some unicode characters it will convert them to the &#<num>; format.

So no conversions into HtmlEntities takes place.



来源:https://stackoverflow.com/questions/43717647/how-to-convert-currency-symbol-to-corresponding-html-entity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!