Tuesday, October 3, 2017

[C#] Encode and decode HTML strings



namespace Vurdalakov
{
    using System;

    public static class StringExtensions
    {
        public static String EncodeHtml(this String s)
        {
            return s.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
        }

        public static String DecodeHtml(this String s)
        {
            return s.Replace("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'");
        }
    }
}


No comments:

Post a Comment