简体   繁体   English

使用.net将字符转换为html等效项

[英]Convert characters to html equivalent using .net

I have a text document that is a roster of licensees. 我有一个文本文件,该文件是被许可人的名册。 I am looping through this document to create a html table of this data. 我正在遍历此文档以创建此数据的html表。 I've come across names with non standard characters. 我遇到了带有非标准字符的名称。

This is one of them 这是其中之一

Aimeé 爱美

I tried running all the inputs through the following function, but when it comes across the above character it doesn't replace it. 我尝试通过以下函数运行所有输入,但是遇到上述字符时,它不会替代它。

Function ReplaceBadCharacters(ByVal input As String) As String
  Return input.Replace(Chr(233), "é")
End Function

How can I replace each character with the html equivalent? 如何将每个字符替换为html等值?

EDIT 编辑

When I debug the above function it shows the input as Aime[] and not Aimeé. 当我调试上面的函数时,它将输入显示为Aime []而不是Aimeé。

In Chrome it looks like this Aime 在Chrome中看起来像这个Aime。

You don't need to do that. 您不需要这样做。
As long as your page is encoded as UTF8, the characters will work fine. 只要您的页面编码为UTF8,字符就可以正常工作。

However, you do need to call Server.HtmlEncode to escape HTML special characters. 但是,您确实需要调用Server.HtmlEncode来转义HTML特殊字符。
(Unless you're printing the strings in a <%: %> block or a Razor @ block, which escapes them for you) (除非您在<%: %>块或Razor @块中打印字符串,否则将为您转义)

é is in the current ASCII char set. é在当前的ASCII字符集中。 If you put that into the HTML, it will render correctly (just like how it shows up correctly in the browser when you look at this page) 如果将其放到HTML中,它将正确呈现(就像查看此页面时在浏览器中正确显示的样子一样)

but if you want to replace all instances of it, use this instead &eacute; 但是如果要替换它的所有实例,请改用&eacute;

input.Replace("é", "&eacute;") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM