IamCalvinBrown.com a Blog Site

Code Examples, Consulting Advice and lots of other cool things

How to remove special characters from a string in C#


public static string RemoveSpecialCharacters(string str)

        {

            StringBuilder sb = new StringBuilder();

            foreach (char c in str)

            {

                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')

                {

                    sb.Append(c);

                }

            }

            return sb.ToString();

        }

Loading