c#去掉HTML所有标签

html-css018

c#去掉HTML所有标签,第1张

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

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 = 0i <dt.Rows.Counti++)

{

DataRow dr = dt.Rows[i]

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

str.Append("<li class='li_KongGe'>" +

"<ul><li>" +

"<img src='images/rili.png' alt=''" + dr["information_Title"] + "'>" +

"<h4>" + Convert.ToDateTime(dr["information_Time"]).ToLongDateString().ToString() + "</h4>" +

"</li>" +

"<li>" +

"<h5><a href='serviceD.html?information_Id=" + dr["information_Id"]+"'>" + dr["information_Title"] + "</a></h5>" +

"<p>" + sqlhelper.ReplaceHtmlTag(content, 40) + "..." + "</p>" +

"</li>" +

"</ul>" +

"</li>")

}

}

return str

}

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

使用正则表达式去掉html标签的方法常用的正则表达式是:/<[^<]+?>/g1、定义含有html标签的字符串:$text='<p>Testparagraph.</p><!--Comment--><ahref="#fragment">Othertext</a>'2、定义正则表达式并替换$val=preg_replace('/<[^<]+?>/g','',$row_get_Business['business_description'])3、截取指定长度$businessDesc=substr(val,0,110)

java处理html指定标签最好用正则表达式。例如要去除html中所有的h1标签和类容就可以用下面的演示代码: package konw.regimport java.io.BufferedReaderimport java.io.BufferedWriterimport java.io.FileNotFoundExceptionimport java.io.F