C#将ISO-8859-1字符转换为实体编号 [英] C# convert ISO-8859-1 characters to entity number

查看:325
本文介绍了C#将ISO-8859-1字符转换为实体编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何将ISO-8859-1字符(例如é)转换为实体编号为é.

I can't seem to figure out how to convert ISO-8859-1 characters, such as é, to it's entity number being é.

我希望能够使用一个字符串,例如:"SteelDécor"

I want to be able to take a string, such as: "Steel Décor"

并将其转换为:"Steel D é cor"

and have it converted to: "Steel Décor"

推荐答案

假定您不关心HTML中特殊的HTML编码字符(例如<,&等),则可以通过一个简单的循环该字符串将起作用:

Assuming you don't care about HTML-encoding characters that are special in HTML (e.g., <, &, etc.), a simple loop over the string will work:

string input = "Steel Décor";
StringBuilder output = new StringBuilder();
foreach (char ch in input)
{
    if (ch > 0x7F)
        output.AppendFormat("&#{0};", (int) ch);
    else
        output.Append(ch);
}
// output.ToString() == "Steel D&#233;cor"

根据您的确切需求,可能需要更改if语句以转义字符< 0x20或非字母数字等.

The if statement may need to be changed to also escape characters < 0x20, or non-alphanumeric, etc., depending on your exact needs.

这篇关于C#将ISO-8859-1字符转换为实体编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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