zoukankan      html  css  js  c++  java
  • 去掉文件名中的非法字符

    今天在做一个文件下载功能时,发现文件名要从数据库中的某个字段去取,而该字段并没有保证不会使用到非法的字符,如 \ / : * ? " < > |

    所有我就自己写了一个简单的方法进行替换,代码如下:

            /// <summary>
            
    /// 去掉文件名中的无效字符,如 \ / : * ? " < > | 
            
    /// </summary>
            
    /// <param name="fileName">待处理的文件名</param>
            
    /// <returns>处理后的文件名</returns>
            public string ReplaceBadCharOfFileName(string fileName)
            {
                
    string str=fileName;
                str
    =str.Replace("\\",string.Empty);
                str
    =str.Replace("/",string.Empty);
                str
    =str.Replace(":",string.Empty);
                str
    =str.Replace("*",string.Empty);
                str
    =str.Replace("?",string.Empty);
                str
    =str.Replace("\"",string.Empty);
                str=str.Replace("<",string.Empty);
                str
    =str.Replace(">",string.Empty);
                str
    =str.Replace("|",string.Empty);
                str
    =str.Replace(" ",string.Empty);    //前面的替换会产生空格,最后将其一并替换掉
                return str;
            }
    问题是最后必须要做一个处理,就是最后会产生空格,没办法再加上一句一并替换了才没出问题。
  • 相关阅读:
    myeclipse 自定义视图Customize Perspective 没有反应
    myEclipse工具栏设置新建类、包、接口图标
    eclipse手动添加SVN插件
    ui 库vue app
    sublime text3安装emmet插件
    Sublime Text 3最受欢迎的配色主题theme-超炫
    516D Drazil and Morning Exercise
    506C Mr. Kitayuta vs. Bamboos
    555E Case of Computer Network
    576D Flights for Regular Customers
  • 原文地址:https://www.cnblogs.com/wubin264/p/1592805.html
Copyright © 2011-2022 走看看