dg word = new dg();//dg是一个类DgName.DataSource = word.dgfunction(Convert.ToInt32(Request["ID"].ToString()));
//dgfunction是类dg中的一个函数,是获取网址中的ID的。DgName.DataBind();//数据的绑定比如:http://community.csdn.net/PostNew.asp?ID=5201。中dgfunction是获取ID5201的。请问:
现在我想获取网址中的文件PostNew.asp的名字PostNew,
该如何获取,该如何写这个获取名字的语句的代码?谢谢

解决方案 »

  1.   

    通过程序来完成。
                string str;
                str = "http://community.csdn.net/PostNew.asp?ID=5201";
                int intStart, intEnd;
                intStart = str.LastIndexOf("/");
                intEnd = str.LastIndexOf(".");
                if (intStart == -1 || intEnd == -1)
                {
                    Console.Write("Error");
                    return;
                }
                str = str.Substring(intStart + 1, intEnd - intStart);
                Console.Write(str);
      

  2.   

    string sUrl=Request.Url.ToString();
            int istartIndex=sUrl.LastIndexOf("/");
            int iendIndex=sUrl.LastIndexOf(".");
            Response.Write(sUrl.Substring(istartIndex+1,iendIndex-(istartIndex+1)));来晚一步
      

  3.   

    using System.Text.RegularExpressions;         string strin = "http://community.csdn.net/PostNew.asp?ID=5201";
            string strout = Regex.Split(strin, ".asp", RegexOptions.IgnoreCase)[0];
            strout = strout.Split('/')[strout.Split('/').Length - 1];
      

  4.   

    Easy way:
    System.IO.Path.GetFileNameWithoutExtension(Request.Url.AbsoluteUri)
      

  5.   

    这样最快
    string strin = "http://community.csdn.net/PostNew.asp?ID=5201";
    string fileName = System.IO.Path.GetFileNameWithoutExtension(strin);
      

  6.   

    System.IO.Path.GetFileNameWithoutExtension() 真是好方法,大家常常需要的