Replace Special Characters in C# using Regex

If you are having a string with special characters and want’s to remove/replace them then you can use regex for that.

Use this code:

Regex.Replace(your String, @"[^0-9a-zA-Z]+", "")
Code language: JavaScript (javascript)

This code will remove all of the special characters ┬ábut if you doesn’t want to remove some of the special character for e.g. comma “,” and colon “:” then make changes like this:

Regex.Replace(Your String, @"[^0-9a-zA-Z:,]+", "")
Code language: JavaScript (javascript)

Leave a Reply