从数据库读出的字段,比如新闻的格式,要做简要显示的时候,需要去掉格式,只显示文本,以下是整理的一个方法,更新中。。。
测试环境:VS2005+.NET 2.0 +C#
1、引用using System.Text.RegularExpressions
2、主要方法
测试环境:VS2005+.NET 2.0 +C#
1、引用using System.Text.RegularExpressions
2、主要方法
1public string RemoveHtml(string strHtml)
2 {
3 string strhtml = Regex.Replace(strHtml, "<.+?>", "");
4 strhtml = Regex.Replace(strhtml, "<br>", "", RegexOptions.IgnoreCase);
5 return strhtml;
6 }
3、全部代码,包括后台:2 {
3 string strhtml = Regex.Replace(strHtml, "<.+?>", "");
4 strhtml = Regex.Replace(strhtml, "<br>", "", RegexOptions.IgnoreCase);
5 return strhtml;
6 }
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text.RegularExpressions;
11
12public partial class _Default : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 TextBox1.Text="<table><tr><td>呵呵,<font color=red>测试</font>一下。。。<br />换行看看</td></tr></table>";
19 }
20 }
21
22 protected void btnFilterHtml_Click(object sender, EventArgs e)
23 {
24 string strhtml = TextBox1.Text;
25 Label1.Text = RemoveHtml(strhtml);
26 Label2.Text = TextBox1.Text;
27 }
28
29 public string RemoveHtml(string strHtml)
30 {
31 string strhtml = Regex.Replace(strHtml, "<.+?>", "");
32 strhtml = Regex.Replace(strhtml, "<br>", "", RegexOptions.IgnoreCase);
33 return strhtml;
34 }
35}
前台:2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text.RegularExpressions;
11
12public partial class _Default : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18 TextBox1.Text="<table><tr><td>呵呵,<font color=red>测试</font>一下。。。<br />换行看看</td></tr></table>";
19 }
20 }
21
22 protected void btnFilterHtml_Click(object sender, EventArgs e)
23 {
24 string strhtml = TextBox1.Text;
25 Label1.Text = RemoveHtml(strhtml);
26 Label2.Text = TextBox1.Text;
27 }
28
29 public string RemoveHtml(string strHtml)
30 {
31 string strhtml = Regex.Replace(strHtml, "<.+?>", "");
32 strhtml = Regex.Replace(strhtml, "<br>", "", RegexOptions.IgnoreCase);
33 return strhtml;
34 }
35}
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:TextBox ID="TextBox1" runat="server" Rows="5" TextMode="MultiLine" Width="434px"></asp:TextBox><br />
13 <asp:Button ID="btnFilterHtml" runat="server" OnClick="btnFilterHtml_Click" Text="过滤掉标签" /><br />
14 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
15 <hr />
16 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
17 </div>
18 </form>
19</body>
20</html>
21
---可以直接复制运行
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:TextBox ID="TextBox1" runat="server" Rows="5" TextMode="MultiLine" Width="434px"></asp:TextBox><br />
13 <asp:Button ID="btnFilterHtml" runat="server" OnClick="btnFilterHtml_Click" Text="过滤掉标签" /><br />
14 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
15 <hr />
16 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
17 </div>
18 </form>
19</body>
20</html>
21