发布网友 发布时间:2023-07-05 03:14
共2个回答
热心网友 时间:2024-12-01 19:43
复制黏贴就可以用了 看看图片是不是你想要的效果
后台代码
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.RegularExpressions;
public partial class test_Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
public int[] FindPositions(string strSource, string strKey)
{
//这里由hlxwell提供
MatchCollection mc = Regex.Matches(strSource, strKey);
string a = "";
foreach (Match m in mc)
{
//把所有位置用,隔开
a += m.Index + ",";
}
//根据,把位置的值分开来
string[] s = a.Split(',');
int[] sPosition = new int[s.Length - 1];
for (int i = 0; i < s.Length - 1; i++)
{
sPosition[i] = Convert.ToInt32(s[i]);
}
return sPosition;
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "";
string tou;
string end;
str = this.ctext.Text;
tou = this.chead.Text.Trim();
end = this.cend.Text.Trim();
try {
int[] number = FindPositions(str, tou);
int len = number[0]+tou.Length;
string subs = str.Substring(len, str.Length - len);
int[] number2 = FindPositions(subs, end);
string subs2 = subs.Substring(0, number2[0]);
Response.Write(subs2); }
catch (Exception ex)
{
Response.Write("截取失败!");
}
}
}
前台<body>:
<body>
<form id="form1" runat="server">
<div>
要截取的内容<br />
<asp:TextBox ID="ctext" runat="server" TextMode="MultiLine" Height="125px" Width="330px"></asp:TextBox><br />
<br />
开头
<asp:TextBox ID="chead" runat="server" Width="89px"></asp:TextBox>结尾
<asp:TextBox ID="cend" runat="server" Width="82px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
</form>
</body>
热心网友 时间:2024-12-01 19:43
有很多方法,如果都是from/to/subject这种形式的,逐行读取并判断是否是某某开头就可以的,当然,也可以用正则表达式查找符合要求的。