How to Replace ‘

I have a string which I'm html encoding, then using a string builder to allow certain html characters. All works fine except for the left sided quote ‘. Any ideas what I'm doing wrong?

StringBuilder htmlStr = new StringBuilder();
htmlStr.Append(HttpUtility.HtmlEncode(reader["NewsDetail"]));
htmlStr.Replace("&lt;p&gt;", "<p>");
htmlStr.Replace("&lt;/p&gt;", "</p>");
htmlStr.Replace("&lsquo;", "‘");
6

1 Answer

This char is not encoded by HtmlEncode.

string h = HttpUtility.HtmlEncode("<p>‘test’</p>");
Console.WriteLine(h);
// output: &lt;p&gt;‘test’&lt;/p&gt;

If you need to encode you will have to do it yourself. Check this post: HttpUtility.HtmlEncode doesn't encode everything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest