• Top Posts

    C# Program to remove special characters from a String


    • By the following example we can remove the special characters from the given string.




      class SpecialChar  
       {  
         public static void Main(string[] args)  
         {  
           string str = "$c!sh$arp&st%ar";  
           Console.WriteLine(RemoveSpecialChars(str));  
           Console.ReadLine();  
         }  
         public static string RemoveSpecialChars(string str)  
         {  
           // Create a string array and add the special characters you want to remove  
           string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "_", "(", ")", ":", "|", "[", "]" };  
           //Iterate the number of times based on the String array length.  
           for (int i = 0; i < chars.Length; i++)  
           {  
             if (str.Contains(chars[i]))  
             {  
               str = str.Replace(chars[i], "");  
             }  
           }  
           return str;  
         }  
       }  
    

    No comments

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728