w3resource

C# Sharp Exercises: Remove special characters from a given text

C# Sharp Regular Expression: Exercise-8 with Solution

Write a C# Sharp program to remove the special characters from a given text. Return the new string which allowed alphanumeric characters, spaces, underscores _ and dashes - .

Note: A special character is a character that is not an alphabetic or numeric character. Special characters are: ., !, @, #, $, %, ^, &, \, *, (, )

Sample Data:
("[email protected]%^&CC") -> "AACC"
("Python") -> "Python"
("w3resource.com") -> "w3resourcecom"

Sample Solution-1:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
namespace exercises
{
   class Program
   {
       static void Main(string[] args)
       {
           string text1 = "[email protected]%^&CC";
           Console.WriteLine("Original string: " + text1);
           Console.WriteLine("New string: " + test(text1));
           text1 = "Python";
           Console.WriteLine("Original string: " + text1);
           Console.WriteLine("New string: " + test(text1));
           text1 = "w3resource.com";
           Console.WriteLine("Original string: " + text1);
           Console.WriteLine("New string: " + test(text1));
       }


       public static string test(string text)
       {
           return Regex.Replace(text, "[^0-9A-Za-z _-]", "");
       }
   }
}

Sample Output:

Original string: [email protected]%^&CC
New string: AACC
Original string: Python
New string: Python
Original string: w3resource.com
New string: w3resourcecom

Flowchart:

Flowchart: C# Sharp Exercises - Remove special characters from a given text.

Sample Solution-2:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
namespace exercises
{
   class Program
   {
       static void Main(string[] args)
       {
           string text = "[email protected]%^&CC";
           Console.WriteLine("Original string: " + text);
           Console.WriteLine("New string: " + test(text));
           text = "Python";
           Console.WriteLine("Original string: " + text);
           Console.WriteLine("New string: " + test(text));
           text = "w3resource.com";
           Console.WriteLine("Original string: " + text);
           Console.WriteLine("New string: " + test(text));
       }


       public static string test(string text)
       {
           return Regex.Replace(text, @"[^\w \-]", "");
       }
   }
}

Sample Output:

Original string: [email protected]%^&CC
New string: AACC
Original string: Python
New string: Python
Original string: w3resource.com
New string: w3resourcecom

Flowchart:

Flowchart: C# Sharp Exercises - Remove special characters from a given text.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Check whether a string represents a currency value.
Next C# Sharp Exercise: Find a specific word in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter