title: C# Remove special characters
body: <p>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 (.).</p>
  
  <p>I have tried this:</p>
  
  <pre><code>        StringBuilder sb = new StringBuilder();
          foreach (char c in input)
          {
              if ((c &gt;= '0' &amp;&amp; c &lt;= '9') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') || (c &gt;= 'a' &amp;&amp; c &lt;= 'z') | c == '.' || c == '_' || c == ' ' || c == '%')
              { sb.Append(c); }
          }
          return sb.ToString();
  </code></pre>
  
  <p>And this:</p>
  
  <pre><code>        Regex r = new Regex("(?:[^a-z0-9% ]|(?&lt;=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); 
          return r.Replace(input, String.Empty); 
  </code></pre>
  
  <p>But nothing seems to be working. Any help will be appreciated.</p>
  
  <p>Thank you!</p>