html 隐藏标签tb显示,c#去掉HTML所有标签

此篇博客介绍了一个用于去除HTML标签并截断长内容的方法,它在后台通过SqlHelper进行数据库查询,提取tb_information表中的信息,并应用ReplaceHtmlTag函数处理文章内容。关键步骤包括使用正则表达式移除标签和空格,确保了内容的简洁展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//去除所有的

public static string ReplaceHtmlTag(string html, int length = 0)

{

string strText = System.Text.RegularExpressions.Regex.Replace(html, "]+>", "");

strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

if (length > 0 && strText.Length > length)

return strText.Substring(0, length);

return strText;

}

这个方法放在公共类中,然后后台调用

public StringBuilder Datatech(string name, string sql)

{

StringBuilder str = new StringBuilder();

DataTable dt = sqlhelper.publicFun("select top 6 * from tb_information where " + sql);

if (dt != null)

{

for (int i = 0; i

{

DataRow dr = dt.Rows[i];

string content = dr["information_Content"].ToString().Trim();

str.Append("

" +

"

  • " +

    "" +

    "

    " + Convert.ToDateTime(dr["information_Time"]).ToLongDateString().ToString() + "
    " +

    "

    " +

"

" +

"

" + dr["information_Title"] + "
" +

"

" + sqlhelper.ReplaceHtmlTag(content, 40) + "..." + "

" +

"

" +

"

" +

"

");

}

}

return str;

}

之所以用 string content = dr["information_Content"].ToString().Trim();的作用是去掉所有空格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值