Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Remove special characters

I want to remove all special characters from a string. Allowed characters are A-Z (uppercase or lowercase), numbers (0-9), underscore (_), white space ( ), pecentage(%) or the dot sign (.).

I have tried this:

        StringBuilder sb = new StringBuilder();
        foreach (char c in input)
        {
            if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') | c == '.' || c == '_' || c == ' ' || c == '%')
            { sb.Append(c); }
        }
        return sb.ToString();

And this:

        Regex r = new Regex("(?:[^a-z0-9% ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); 
        return r.Replace(input, String.Empty); 

But nothing seems to be working. Any help will be appreciated.

Thank you!

like image 641
OBL Avatar asked Apr 15 '11 18:04

OBL


2 Answers

Regex.Replace(input, "[^a-zA-Z0-9% ._]", string.Empty)
like image 152
Sanjeevakumar Hiremath Avatar answered Oct 05 '22 20:10

Sanjeevakumar Hiremath


You can simplify the first method to

StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
    if (Char.IsLetterOrDigit(c) || c == '.' || c == '_' || c == ' ' || c == '%')
    { sb.Append(c); }
}
return sb.ToString();

which seems to pass simple tests. You can shorten it using LINQ

return new string(
    input.Where(
        c => Char.IsLetterOrDigit(c) || 
            c == '.' || c == '_' || c == ' ' || c == '%')
    .ToArray());
like image 42
Yuriy Faktorovich Avatar answered Oct 05 '22 21:10

Yuriy Faktorovich