Removes any illegal file characters from a string vis Regex - CSharp File IO

CSharp examples for File IO:File Command

Description

Removes any illegal file characters from a string vis Regex

Demo Code


using System.Text.RegularExpressions;
using System.IO;/*from  www.j a  v  a 2 s . c  om*/
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Removes any illegal file characters from a string
        /// </summary>
        public static string RemoveIllegalCharacters(string str)
        {   //Remove characters...
            string c = str;
            Regex regex = new Regex(@"(\/|\*|\?|\||:|<|>)", RegexOptions.None);
            c = regex.Replace(c, @".");
            //Trim it
            str = c.Trim();
            //We have our new Infantry compatible string!
            return str;
        }
}

Related Tutorials