CSharp examples for System:String HTML
HTML-encodes a string and returns the encoded string.
using System.Text; using System.Globalization; using System;/* ww w . java 2s. c o m*/ public class Main{ /// <summary> /// HTML-encodes a string and returns the encoded string. /// </summary> /// <param name="text">The text string to encode. </param> /// <returns>The HTML-encoded text.</returns> public static string HtmlEncode(this string text) { if (text == null) return null; var sb = new StringBuilder(text.Length); var len = text.Length; for (var i = 0; i < len; i++) { switch (text[i]) { case '<': sb.Append("<"); break; case '>': sb.Append(">"); break; case '"': sb.Append("""); break; case '&': sb.Append("&"); break; default: if (text[i] > 159) { // decimal numeric entity sb.Append("&#"); sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture)); sb.Append(";"); } else sb.Append(text[i]); break; } } return sb.ToString(); } }