Smart little helper to clean HTML based strings for [WPDEV] and [WIN8DEV]

I thought I would share this small little class I created to clean out HTML based strings into readable strings. The class is both usable for your Windows Phone app as well as your Windows 8 app.

If you use cloud based or internet data, often strings contain “–” or “–” instead of their intended letter, like you can see on this image:

htmlstringuncleaned

The method uses Regex to replace all the wrong signs and clean out all of the unwanted signs. You can also add more to that if you need to replace wrong letters.

class CleanHTML
    {      
        public static string RemoveEncoding(string text)
        {
            try
            {
                string temp="";

                temp = 
                    Regex.Replace
                    (text.
                    Replace("–", "-").
                    Replace(" ", " ").
                    Replace("’", "'").
                    Replace("&", "&").
                    Replace("&", "&").
                    Replace(""", """).
                    Replace("'", "'").
                    Replace("…", "...").
                    Replace("—", "—").
                    Replace("–", "-").
                    Replace("“", "“").
                    Replace("”", "”").
                    Replace("’", "'").
                    Replace(" ", " ").
                    Replace(">", ">").
                    Replace("”", """).
                    Replace("“", """).
                    Replace("&lt;", "<").
                    Replace("&#215;", "×").
                    Replace("&#8242;", "′").
                    Replace("&#8243;", "″").
                    Replace("&#8216;", "'"),
                    "<[^<>]+>", "");

                return temp;
            }
            catch
            {
                return "";
            }
        }

        }

As we created a class for this little helper, you can call it from everywhere within you app to clean out the string. Here is one example I am using:

 item.title = CleanHTML.RemoveEncoding(item.title);

After calling this method, your string is plain text:

Screenshot (17)

I hope this post will be helpful for some of you.

Happy coding!

Leave a Reply