Reputation: 740
I am trying to replace a string that includes a parenthesis in my C# email script with a given variable. A snippet of my code inside a function goes like this:
MailDefinition md = new MailDefinition();
md.From = "[email protected]";
string addressee = salutation;
ListDictionary replacements = new ListDictionary();
replacements.Add("\n", "<br />");
replacements.Add("(Name of Employee)", addressee)
string body = EmployeeQuery.getEmailMessageByCategory(category, subcategory);
MailMessage msg = md.CreateMailMessage("[email protected]", replacements, body, new System.Web.UI.Control());
the main issue is this part of the code:
replacements.Add("(Name of Employee)", addressee)
the "Name of Employee" gets replaced by the value of addressee variable. However, it doesn't replace the parenthesis.
How can I replace the parenthesis as well so that only the value of addressee is added? TIA
Upvotes: 0
Views: 2561
Reputation: 3802
Use This :
replacements.Add("\\(Name of Employee\\)", addressee)
// \ acts as escape charecter, supressing the special meaning of next charecter
// (XYZ) matches XYZ as a group
// \\(XYZ\\) matches (XYZ)
Upvotes: 1
Reputation: 2320
You need to put "\\(" and "\\)". @denims answer is probably partly correct, but c# itself interprets the \ in the string. You probably need the \ to be interpreted by the method you're calling.
That said the method documentation doesn't indicate why this is happening in the first place.
Upvotes: 2