SoFunction
Updated on 2025-04-08

Share HTML character conversion function in C#


///<summary>
///Replace special characters in html
///</summary>
///<paramname="theString">text that needs to be replaced. </param>
///<returns> replaced text. </returns>
public static string HtmlEncode(string theString)
{
theString=(">","&gt;");
theString=("<","&lt;");
theString=(" ","&nbsp;");
theString=("\"","&quot;");
theString = ("\'", "&#39;");
theString=("\n","<br/>");
return theString;
}

///<summary>
///Restore special characters in html
///</summary>
///<paramname="theString">Texts that need to be restored. </param>
///<returns>Restore good text. </returns>
public static string HtmlDiscode(string theString)
{
theString=("&gt;",">");
theString=("&lt;","<");
theString=("&nbsp;"," ");
theString=("&quot;","\"");
theString = ("&#39;", "\'");
theString=("<br/>","\n");
return theString;
}