|
执行结果为: 力 量:0智 慧:945
页面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" Height="272px" TextMode="MultiLine"
Width="334px"></asp:TextBox><br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br /> <br /> <asp:TextBox ID="TextBox2" runat="server" Height="272px" TextMode="MultiLine"
Width="334px"></asp:TextBox><br /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div> </form> </body> </html>
源代码: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; using System.Text.RegularExpressions;
public partial class test2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
} protected void Button1_Click(object sender, EventArgs e) { string s=TextBox1.Text; string liliang = return_str(s, "力 量:"); string zihui = return_str(s, "智 慧:");
TextBox2.Text = "力 量:" + liliang + "智 慧:" + zihui; }
private string return_str(string str,string xiang) { //Regex r = new Regex("力 量:" + "[^<]+", RegexOptions.IgnoreCase); Regex r = new Regex(xiang + "[^<]+", RegexOptions.IgnoreCase); Match m; m = r.Match(str);
string temp=""; if (m.Success) { temp = m.Value.Trim(); if (temp!=null&&temp != "") { temp = temp.Replace(xiang, "").Replace(" ","");//去掉"力 量:"等,只要数据 } } return temp; } }
|