如何用重音符号替换HTML表示形式 [英] How to replace accented characters by their HTML representation

查看:92
本文介绍了如何用重音符号替换HTML表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将rég之类的字符串转换为 gr& eacute; gou。

I would like to transform strings like "rég" to "grégou".

我暂时编写了一些代码来手动更改最常见的重音符号,但是我想得到一个将每个重音符号转换为html等效符号的代码。

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.

有人有主意吗? :)

ps:我尝试了一些操作,但是它不起作用...

ps: I tried something but it does not work ...

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

推荐答案

HttpUtility.HtmlEncode方法不会修改参数(C#中的字符串是不可变的!);它将编码后的版本作为新字符串返回:

The HttpUtility.HtmlEncode Method does not modify the argument (strings in C# are immutable!); it returns the encoded version as a new string:

string encoded = HttpUtility.HtmlEncode("rég");

在MVC上下文中编码文本的首选方法似乎是 Html.Encode帮助器方法

The preferred way to encode text in the context of MVC seems to be the Html.Encode Helper Method:

<%= Html.Encode("rég") %>

这篇关于如何用重音符号替换HTML表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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