在Episerver中截断Xhtmlstring [英] Truncating Xhtmlstring in Episerver

查看:99
本文介绍了在Episerver中截断Xhtmlstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取截断的Xhtmlstring的html友好版本,因为标记结尾在截断时可能会被剪切.关于如何实现这一目标的任何想法?我曾经考虑过先删除所有标签,然后剪切,但是在Episerver中是否有解决方案?或者这只是使用正则表达式进行基本的字符串操作?

I'd need to get a html friendly version of a truncated Xhtmlstring as the tag endings might get clipped when truncated. Any ideas on how to achieve this? I've thought of just getting rid of all tags first and then clipping but is there a solution for this inside episerver or is this just basic string-manipulation with regex?

推荐答案

TextIndexer 类中有一个名为 StripHtml 的内置帮助程序功能,删除所有标签以在截断之前以纯文本结尾:

There is a built-in helper function in the TextIndexer class called StripHtml which can be used to remove any tags to end up with plain text before truncating:

var plainText = TextIndexer.StripHtml(someHtml);

请注意,此方法也可以用于截断,如下所示:

Note that this method can also be used to truncate the string like so:

// Truncate to 150 characters
var truncatedString = TextIndexer.StripHtml(someHtml, 150);

如果该字符串被截断,您还可以在其后附加一个字符串,例如"...".

You'll also be able to have a string such as "..." appended to the string if it was truncated.

这篇关于在Episerver中截断Xhtmlstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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