日期:2014-05-18  浏览次数:20745 次

请教各位有关字符窜的截取问题?
/cc/web/File/20077171011481/new1.aspx
想得到 File/20077171011481 或/File/20077171011484/

------解决方案--------------------
C# code
        string a = "/cc/web/File/20077171011481/new1.aspx";
        int b = a.IndexOf("File");
        int c = a.LastIndexOf("/");
        Response.Write(a.Substring(b, c - b) + "<br>");// 输出结果是:File/20077171011481 
        Response.Write(a.Substring(b - 1, c - b + 2)); // 输出结果是:/File/20077171011481/

------解决方案--------------------
还可以使用正则:
C# code

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 Default13 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string a = "/cc/web/File/20077171011481/new1.aspx";
        string reg = @"File/\d{14}";
        MatchCollection mc = Regex.Matches(a, reg);
        foreach (Match m in mc)
        {
            Response.Write(m.Value);  // 输出结果是:File/20077171011481 
        }
   }
}