【发布时间】:2018-02-04 11:38:43
【问题描述】:
现在我有一个带有 html 标签的字符串,例如
<div> <p> and <br>
当然也有可读文本.. 现在我要返回的是可读文本但只有 100 个字符.. 我尝试了以下选项,但没有帮助。
- 正则表达式删除标签。 (给我留下额外的空格或空空格)
- 子字符串 (0, 100)。 (当然,它也将 html 标签读取为字符)
注意:我不能使用 javascript。
下面是我的示例字符串..
<div class="ExternalShit23429812"><p>This is a paragraoph.. </p><br><p>What</p><p>Yeah right</p><p>Paragraph</p><p>and Paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph yes yes yes</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p><p>More paragraph</p></div>
【问题讨论】:
-
使用 mshtml 并玩转元素
-
如果您想精确计算前 100 个字符,您应该使用 Javascript 或服务器端解决方案。
-
如果您使用的是 C#,请使用 AngleSharp 作为类似 jquery 的界面:github.com/AngleSharp/AngleSharp
-
到目前为止你写了什么代码?你在努力什么?
-
使用 HtmlAgilityPack 并使用
public string getCleanHtml(string html) { var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); return HtmlAgilityPack.HtmlEntity.DeEntitize(doc.DocumentNode.InnerText); }获取干净的 HTML 文本,然后获取从 0 到 100 的子字符串。