I am generating some text to be shown on a web-site, and use HttpUtility.HtmlEncode to ensure it will look correct. However, this method does not appear to encode the Em Dash (it should convert it to “—”).
I have come up with a solution, but I’m sure there is a better way of doing it – some library function or something.
sWebsiteText = _
"<![CDATA[" & _
HttpUtility.HtmlEncode(sSomeText) & _
"]]>"
'This is the bit which seems "hacky"'
sWebsiteText = _
sWebsiteText.Replace(HttpUtility.HtmlDecode("–"), "–")
So my question is – how would you implement the “hacky” part?
Many thanks,
RB.
Bobince’s answer gives a solution to what seems to be your main concern : replacing your use of HtmlDecode by a more straightforward declaration of the char to replace.
Rewrite
as
(‘\u2014’ (dec 8212) is em dash, ‘\u2013’ (dec 8211) is en dash.)
For readability purpose it may be considered better to use “–” rather than “–”, since the .Net declaration for the char (“\u2013”) is in hex too. But, as decimal notation seems more common in html, I personaly would prefer using “–”.
For reuse purpose, you probably should write your own HtmlEncode function declared in a custom HttpUtility, in order to be able to call it from anywhere else in your site without duplicating it.
(Have something like (sorry I have written it in C#, forgetting your examples were in VB):
Then replace
With:
)