I have to encode strings to remove parentheses for Ids for HTML elements.
Parentheses (these ones (
,)
) aren't valid in HTML Ids, are there standard strings (like those used in URLs) to use?
Is there an existing method that can be used in ASP.NET MVC?
N.B. System.Web.Mvc.HttpUtility.HtmlEncode(string)
, does not encode parentheses.
As per the HTML specification (and this question about id's) parentheses aren't allowed in the HTML id
attribute. If you need them, you could use string replace, e.g.:
// ( = 'op--' Opening Parenthesis
// ) = 'cp--' Closing Parenthesis
string id = "collectionName.get_Item(index)";
// encode
string encodedId = id.Replace("(", "op--").Replace(")", "cp--");
// decode
string decodedId = encodedId.Replace("op--", "(").Replace("cp--", ")");