How to encode Trademark symbols ™ in C#

Hello Folks, Sorry was away for a while. As was very busy with Project.Challenge:
We were facing one strange issue with encoding Trademark symbol in C#. If you are also facing such similar issue then this post is for you.It was strange for us see that ™ isn’t encoding with HttpUtility.HttpEncode. As google is my best friend [so as yours], I tried to get the answer.Before we dig into the solution, I tried on following websites which help us to get the result live for the Http.HtmlEncode()http://htmlencode.org/
http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx

And we see that HTML Encode isn’t encoding ™.

Solution:
#1: 
I referred three articles by which I found the solution.
1) http://stackoverflow.com/questions/2865273/asp-net-server-htmlencode-wont-encode-%E2%82%AC
– It answered me the problem. WHY.
– [HttpUtility.HtmlEncode only encodes characters that are “reserved” in HTML. For that list, see the first table on this page: https://www.w3schools.com/html/html_entities.asp
In other words, only those characters that can conflict with the basic structre of HTML (e.g. <, >, “, etc). No other characters must be encoded as long as the encoding of the transmitted bytes is identified correctly (e.g. by using and declaring UTF-8).] [Html entities link is broken. You can have a look at:http://www.w3schools.com/html/html_entities.asp]

2) http://stackoverflow.com/questions/7178695/c-sharp-string-replace-not-finding-replacing-symbol-%E2%84%A2-%C2%AE
– It answered me the solution. HOW.
– [Try myString.Replace(“u00A9”, “else”); you have to escape the ©]

3) http://www.fileformat.info/info/unicode/char/2122/index.htm
– It gave me the unicode value of ™.
– [“u2122“]

Html Encode

#2:

Full Html Character Encoding
http://www.codeproject.com/Articles/20255/Full-HTML-Character-Encoding-in-C

public static string HtmlEncode( string text ) {
    char[] chars = HttpUtility.HtmlEncode( text ).ToCharArray();
    StringBuilder result = new StringBuilder( text.Length + (int)( text.Length * 0.1 ) );

    foreach ( char c in chars ) {
        int value = Convert.ToInt32( c );
        if ( value > 127 )
            result.AppendFormat("&#{0};",value); 
        else
            result.Append( c );
    }

    return result.ToString();
}

Select any solution you like. If your string contains more such entities than it will be good to go with solution #2 else go with solution #1.

If you know any other better approach than do share with us. As sharing is caring in IT.

Happy Coding! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.