Tuesday, 4 September 2012

How to remove illegal characters from a string?

public static string RemoveIllegalCharacters(string stringValue)
       {
           string pattern = @"[^\w\s\-\+]";
           string replacement = " ";
           string noResultText = "noresult";

           Regex regEx = new Regex(pattern);
           string sanitized = regEx.Replace(stringValue, replacement);
           if (!string.IsNullOrEmpty(sanitized.Trim()))
               return sanitized;
           else
               return noResultText;
       }

No comments:

Post a Comment