Replace the string of special characters in C#
I believe, best is to use a regular expression here as below
s/[*'",_&#^@]/ /g
You can use Regex
class for this purpose
Regex reg = new Regex("[*'\",_&#^@]");
str1 = reg.Replace(str1, string.Empty);
Regex reg1 = new Regex("[ ]");
str1 = reg.Replace(str1, "-");
Use Regular Expression
Regex.Replace("Hello*Hello'Hello&Hello@Hello Hello", @"[^0-9A-Za-z ,]", "").Replace(" ", "-")
It will replace all special characters with string.Empty and Space with "-"
Make a collection of changes to make and iterate over it:
var replacements = new []
{ new { Old = "*", New = string.Empty }
// all your other replacements, removed for brevity
, new { Old = " ", New = "-" }
}
foreach (var r in replacements)
{
Charseparated = Charseparated.Replace(r.Old, r.New);
}