c# 替换非法字符

保存文件的时候,文件名不允许非法字符。

 

        public string ReplaceSpecialCharacter(string str)
        {
            List<char> charArr = new List<char>() { '\\', '/', '*', '?', '"', '<', '>', '|', ':' };
            return charArr.Aggregate(str, (current, c) => current.Replace(c, ' '));
        }

 

 

public string ReplaceSpecialCharacterV2(string str)
{
List<string> charArr = new List<string>() { "\\", "/", "*", "?", "<", ">", "|", ":" ,"\""};
return charArr.Aggregate(str, (current, c) => current.Replace(c, ""));
}

posted @ 2015-11-25 15:34  pengbg  阅读(413)  评论(2编辑  收藏