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()

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

I referred three articles by which I found the solution.
– 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:
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:]

– It answered me the solution. HOW.
– [Try myString.Replace(“u00A9”, “else”); you have to escape the ©]

– It gave me the unicode value of ™.
– [“u2122“]

Html Encode


Full Html Character Encoding

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.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.