Html Encode - CSharp System

CSharp examples for System:String HTML

Description

Html Encode

Demo Code


using System;//  w w  w .j  a v  a 2 s .  c o m

public class Main{
        public static String HtmlEncode(String source, Int32 spaceCount, Boolean replaceTab, Boolean replaceEnter)
        {
            if (String.IsNullOrEmpty(source))
            {
                return String.Empty;
            }

            String dest = source;

            dest = dest.Replace("&", "&");
            dest = dest.Replace("<", "&lt;");
            dest = dest.Replace(">", "&gt;");
            dest = dest.Replace("\"", "&quot;");

            if (spaceCount > 0)
            {
                String srcSpace = new String(' ', spaceCount);
                String dstSpace = srcSpace.Replace(" ", "&nbsp;");
                dest = dest.Replace(srcSpace, dstSpace);
            }

            if (replaceTab)
            {
                dest = dest.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
            }

            if (replaceEnter)
            {
                dest = dest.Replace(Environment.NewLine, "<br/>");
            }

            return dest;
        }
        public static String HtmlEncode(String source)
        {
            return HtmlEncode(source, 0, false, false);
        }
}

Related Tutorials