Answers for "c# replace characters in string that are invalid using regex"

C#
0

c# replace characters in string that are invalid using regex

string pattern = "[\\~#%&*{}/:<>?|\"-]";
string replacement = " ";

Regex regEx = new Regex(pattern);
string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");

This will replace runs of whitespace with a single space as well.
Posted by: Guest on June-16-2021

Code answers related to "c# replace characters in string that are invalid using regex"

C# Answers by Framework

Browse Popular Code Answers by Language