如何将货币符号转换为相应的HTML实体 [英] How to convert currency symbol to corresponding HTML entity

查看:126
本文介绍了如何将货币符号转换为相应的HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何将€(或任何其他货币符号)转换为相应的html实体.

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

在此示例中€ => €

我正在使用.Net 4.6.1

I'm using .Net 4.6.1

推荐答案

HtmlEncode仅查找一些特殊字符,并将其替换为硬编码值,此外,还另一些其他高ASCII字符(160-255)作为在此处中进行了描述.编码为实体名称的唯一方法是手动指定它们.我试了一下,围绕System.Net.WebUtility类构建了一个包装器,同时利用了.NET使用的现有Html实体数据集进行解码,从而使解码继续适用于此解决方案.我已将其托管在github上: WebUtilityWrapper .您可以按如下所示使用它:

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 €¢£¥

我已经通过编码和解码回去,然后验证我们是否获得了针对大量Unicode字符的原始字符串进行了测试.分享更多测试:

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()使用框架的HtmlEncode的响应:(链接)

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

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

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

// Unicodes for 9824 - 9830
♠♣♥♦

HtmlEncode()使用WebUtilityWrapper.HtmlEncode的响应:

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

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

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

// Unicodes for 9824 - 9830
♠♣♥♦

希望这会有所帮助!

这篇关于如何将货币符号转换为相应的HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆