文件系列--截取路径字符串,获取文件名


现在使用的Word 或Excel都有两种格式,以Word为例,".doc" 或者".docx",取Word不带后缀的文件名涉及到字符串的截取;

    需求是截取Word的文件名,然后换成".pdf的后缀",下面是具体的字符串截取的方式;

1.使用Split 和 Substring组合,截成数组;

/// <summary>
        /// 绝对路径转相对路径
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        private static string urlConvertor(string strUrl)
        {
            string tmpRootDir = HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string urlPath = strUrl.Replace(tmpRootDir, ""); //转换成相对路径
            urlPath = urlPath.Replace(@"/", @"/");
            return urlPath;
        }
 
        /// <summary>
        /// 相对路径转绝对路径
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        private static string urlConvertorLocal(string strUrl)
        {
            string tmpRootDir = HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string urlPath = tmpRootDir + strUrl.Replace(@"/", @"/"); //转换成绝对路径
            return urlPath;
}

 

2.使用Substring 和 LastIndexOf组合,截取字符串;

下面的这种方式简单粗暴;
    class Program
    {
        static void Main(string[] args)
        {
            string str = "fxq.5.6.doc";    //文件名称中设计多个特定符号;
            str = str.Substring(0, str.LastIndexOf("."));
            Console.WriteLine(str);
            Console.Read();
        }

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM