Escapes the string for showing as HTML. Converts enters to <br>, '<' to '&lt;' etc. - CSharp System

CSharp examples for System:String HTML

Description

Escapes the string for showing as HTML. Converts enters to <br>, '<' to '&lt;' etc.

Demo Code


using System.Xml.Serialization;
using System.Collections;
using System.Globalization;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;
using System;//from w  w w. j  av  a2 s  .  c  o m

public class Main{
        /// <summary>
		/// Escapes the string for showing as HTML.
		/// Converts enters to &lt;br/&gt;, '&lt;' to '&amp;lt;' etc.
		/// </summary>
		/// <param name="txt">The Text to be escaped.</param>
		/// <returns></returns>
		public static string EscapeStringForHTML(string txt)
		{
			string res = txt;
			string[] escapeFrom = new string[] { "<", ">", "\n" };
			string[] escapeTo = new string[] { "&lt;", "&gt;", "<br/>" };
			for (int i = 0; i < escapeFrom.Length; ++i)
				res = res.Replace(escapeFrom[i], escapeTo[i]);
			return res;
		}
}

Related Tutorials