Return to Snippet

Revision: 26345
at April 23, 2010 16:48 by kat


Initial Code
/// <summary>
    /// Removes "unsafe" characters from the string.
    /// </summary>
    /// <param name="unsafeString">String to edit</param>
    /// <returns>A "safe" string</returns>
    public static string GetSafeString(string unsafeString)
    {
      // Grab the system invalid chars 
      // Then remove the chars in our IgnoreInvalidChars collection
      char[] systemInvalidChars = Path.GetInvalidFileNameChars();
      List<char> unacceptableChars = new List<char>();
      unacceptableChars.AddRange(systemInvalidChars);

      List<char> okayChars = new List<char>();
      okayChars.AddRange(IgnoreInvalidChars);
      
      // Remove the items in unaccpetableChars
      // that make okayChars.Contains(char) return true
      unacceptableChars.RemoveAll(okayChars.Contains);

      List<char> needEscapeChars = new List<char>();
      needEscapeChars.AddRange(NeedEscapeInRegexChars);

      string regexPattern = StartingRegexPattern;

      // Write each char into a string
      // some chars need to be further escaped
      // for regex to handle them.
      for (int i = 0; i < unacceptableChars.Count; i++)
      {
        //If the char needs to have the escape char in front of it
        // add the escape char
        if (needEscapeChars.Contains(unacceptableChars[i]))
        {
          regexPattern = regexPattern + RegexEscapeString;
        }
        regexPattern = regexPattern + unacceptableChars[i];
      }

      regexPattern = regexPattern + ClosingRegexPattern;

      return Regex.Replace(unsafeString.Trim(), regexPattern, string.Empty);
    }


private static readonly string StartingRegexPattern = "[";
    private static readonly string ClosingRegexPattern = "]";
    private static readonly string RegexEscapeString = @"\";

    private static readonly char[] IgnoreInvalidChars = 
      { '\n', '\r', '\a', '\b', '\t', '\0', '\v', '\f' };

    private static readonly char[] NeedEscapeInRegexChars = 
      { '\\', '/' };

Initial URL


Initial Description
Remove illegal chars from a string using regular expressions

Initial Title
Removing bad chars from a string

Initial Tags


Initial Language
C#