Removing bad chars from a string


/ Published in: C#
Save to your folder(s)

Remove illegal chars from a string using regular expressions


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Removes "unsafe" characters from the string.
  3. /// </summary>
  4. /// <param name="unsafeString">String to edit</param>
  5. /// <returns>A "safe" string</returns>
  6. public static string GetSafeString(string unsafeString)
  7. {
  8. // Grab the system invalid chars
  9. // Then remove the chars in our IgnoreInvalidChars collection
  10. char[] systemInvalidChars = Path.GetInvalidFileNameChars();
  11. List<char> unacceptableChars = new List<char>();
  12. unacceptableChars.AddRange(systemInvalidChars);
  13.  
  14. List<char> okayChars = new List<char>();
  15. okayChars.AddRange(IgnoreInvalidChars);
  16.  
  17. // Remove the items in unaccpetableChars
  18. // that make okayChars.Contains(char) return true
  19. unacceptableChars.RemoveAll(okayChars.Contains);
  20.  
  21. List<char> needEscapeChars = new List<char>();
  22. needEscapeChars.AddRange(NeedEscapeInRegexChars);
  23.  
  24. string regexPattern = StartingRegexPattern;
  25.  
  26. // Write each char into a string
  27. // some chars need to be further escaped
  28. // for regex to handle them.
  29. for (int i = 0; i < unacceptableChars.Count; i++)
  30. {
  31. //If the char needs to have the escape char in front of it
  32. // add the escape char
  33. if (needEscapeChars.Contains(unacceptableChars[i]))
  34. {
  35. regexPattern = regexPattern + RegexEscapeString;
  36. }
  37. regexPattern = regexPattern + unacceptableChars[i];
  38. }
  39.  
  40. regexPattern = regexPattern + ClosingRegexPattern;
  41.  
  42. return Regex.Replace(unsafeString.Trim(), regexPattern, string.Empty);
  43. }
  44.  
  45.  
  46. private static readonly string StartingRegexPattern = "[";
  47. private static readonly string ClosingRegexPattern = "]";
  48. private static readonly string RegexEscapeString = @"\";
  49.  
  50. private static readonly char[] IgnoreInvalidChars =
  51. { '\n', '\r', '\a', '\b', '\t', '\0', '\v', '\f' };
  52.  
  53. private static readonly char[] NeedEscapeInRegexChars =
  54. { '\\', '/' };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.