如何用 HTML 表示形式替换重音字符
我想将“rég”等字符串转换为“grégou”。
我临时编写了一些代码来手动更改最常见的重音,但我希望获得一个将每个重音转换为其 html 等效项的代码。
有人有主意吗? :)
ps: 我尝试了一些东西,但它不起作用...
C# 代码:
public static MvcHtmlString MyEncode(this HtmlHelper htmlHelper, string text)
{
StringBuilder builder = new StringBuilder();
Byte[] bArray;
HttpUtility.HtmlEncode(text);
bArray = System.Text.Encoding.GetEncoding(850).GetBytes(text);
String chaine = "";
for(int i=0; i<bArray.Length; i++)
{
chaine = chaine + (char)bArray[i];
}
HttpUtility.HtmlEncode(chaine);
builder.Append(chaine);
return MvcHtmlString.Create(builder.ToString());
}
--OLD
I would like to transform strings like "rég" to "grégou".
I temporarily wrote some code that manually changes the most common accents, but I would like to get one that transforms each accent to its html equivalent.
Someone has an idea? :)
ps: I tried something but it does not work ...
C # code:
public static MvcHtmlString MyEncode(this HtmlHelper htmlHelper, string text)
{
StringBuilder builder = new StringBuilder();
Byte[] bArray;
HttpUtility.HtmlEncode(text);
bArray = System.Text.Encoding.GetEncoding(850).GetBytes(text);
String chaine = "";
for(int i=0; i<bArray.Length; i++)
{
chaine = chaine + (char)bArray[i];
}
HttpUtility.HtmlEncode(chaine);
builder.Append(chaine);
return MvcHtmlString.Create(builder.ToString());
}
--OLD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HttpUtility.HtmlEncode 方法 不会修改参数(C# 中的字符串为不可变!);它将编码版本作为新字符串返回:
在 MVC 上下文中对文本进行编码的首选方式似乎是 Html.Encode 辅助方法:
The HttpUtility.HtmlEncode Method does not modify the argument (strings in C# are immutable!); it returns the encoded version as a new string:
The preferred way to encode text in the context of MVC seems to be the Html.Encode Helper Method:
库 HelperSharp 有一个用于此目的的方法:EscapeAccentsToHtmlEntities
The library HelperSharp has a method for this purpose: EscapeAccentsToHtmlEntities
快速谷歌搜索“HTML实体编码C#”会带来很多点击......如下所示:
http://www.codeproject.com/KB/recipes/htmlencodingcsharp.aspx
还有执行此功能的框架类:
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx
A quick google search for "HTML entity encode C#" brings up lots of hits ... like the following:
http://www.codeproject.com/KB/recipes/htmlencodingcsharp.aspx
There are also framework classes that perform this function:
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx