Advertisement

Latest Post

Sunday, 18 September 2016

Remove Special Chars from string using C#

Hello everyone, I am sharing  the code to remove special characters from  string using the ASP.Net C#.

private static string RemoveSpecialChars(string str)
 {
     string[] chars = new string[]{ ",", ".", "/", "!", "@", "#", "$", "%", "^", "&",                                       "*", "'", "\"", ";", "_", "(", ")", ":", "|", "[", "]" };
       for (int i = 0; i < chars.Length; i++)
        {
             if (str.Contains(chars[i]))
                {
                    str = str.Replace(chars[i], "");
                }
         }
  char[] arr = str.Where(c =>(char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-')).ToArray();

   str = new string(arr);
   return str;

 }

Government Jobs