首页 > 解决方案 > 读取 Delphi 字符串中的 HTML 特殊字符

问题描述

我有一个使用 Expression Web 4 构建的网页“index.html”,其中包含一个用 id 分隔的值:

<html>
<head></head>
<body>
<... some html code ...>
<!--MYVALUEID-->
Dernières News
<... some html code ...>
</body>
</html>

使用我的 delphi 应用程序,我将页面加载到 TStringList 并将值读取到 TEDIT :

S:=TStringList.Create;
S.LoadFromFile('path\index.html');
Edit1.Text:=S[S.IndexOf('<!--MYVALUEID-->')+1];
S.Free;

问题是重音字符,因为我在 TEDIT 中得到了这个:“Dernières News”

在 Expression Web 代码中,文本是正确的:Dernières 新闻

当我在记事本中打开 index.html 时,它会显示:Dernières News

记事本中的文件显示为UTF8

当使用 HTTPApp.HTMLDecode() 我得到了:Dernières News

还有 System.NetEncoding、TNetEncoding.HTML.Decode :Dernières News

是否有可靠的例程来解码 html 特殊字符转换?

我在 SO 中检查了许多问题并尝试了上述解决方案,但没有任何反应。

提前谢谢,我卡住了。

标签: htmldelphispecial-charactersdecode

解决方案


由于您的 HTML 文件以 UTF-8 编码,因此您应该在调用时指定它LoadFromFile()

S := TStringList.Create;
S.LoadFromFile('path\index.html', TEncoding.UTF8);

否则使用 ANSI 编码。


推荐阅读