CSharp examples for System:String HTML
Encode Value as HTML
using System.Text; using System.IO;// w w w.j av a 2 s.c o m using System.Globalization; using System; public class Main{ public static string EncodeValue(string value) { StringBuilder builder = new StringBuilder(); StringReader reader = new StringReader(value); for (int i = reader.Read(); i != -1; i = reader.Read()) { switch (i) { case 0x20: { builder.Append(" "); continue; } case 0x22: { builder.Append("\""); continue; } case 0x26: { builder.Append("&"); continue; } case 60: { builder.Append("<"); continue; } case 0x3e: { builder.Append(">"); continue; } case 0x80: { builder.Append("€"); continue; } case 160: { builder.Append(" "); continue; } case 0xa1: { builder.Append("¡"); continue; } case 0xa2: { builder.Append("¢"); continue; } case 0xa3: { builder.Append("£"); continue; } case 0xa4: { builder.Append("¤"); continue; } case 0xa5: { builder.Append("¥"); continue; } case 0xa6: { builder.Append("¦"); continue; } case 0xa7: { builder.Append("§"); continue; } case 0xa8: { builder.Append("¨"); continue; } case 0xa9: { builder.Append("©"); continue; } case 170: { builder.Append("ª"); continue; } case 0xab: { builder.Append("«"); continue; } case 0xac: { builder.Append("¬"); continue; } case 0xad: { builder.Append("­"); continue; } case 0xae: { builder.Append("®"); continue; } case 0xaf: { builder.Append("¯"); continue; } case 0xb0: { builder.Append("°"); continue; } case 0xb1: { builder.Append("±"); continue; } case 0xb2: { builder.Append("²"); continue; } case 0xb3: { builder.Append("³"); continue; } case 180: { builder.Append("´"); continue; } case 0xb5: { builder.Append("µ"); continue; } case 0xb6: { builder.Append("¶"); continue; } case 0xb7: { builder.Append("·"); continue; } case 0xb8: { builder.Append("¸"); continue; } case 0xb9: { builder.Append("¹"); continue; } case 0xba: { builder.Append("º"); continue; } case 0xbb: { builder.Append("»"); continue; } case 0xbc: { builder.Append("¼"); continue; } case 0xbd: { builder.Append("½"); continue; } case 190: { builder.Append("¾"); continue; } case 0xbf: { builder.Append("¿"); continue; } case 0xc0: { builder.Append("À"); continue; } case 0xc1: { builder.Append("Á"); continue; } case 0xc2: { builder.Append("Â"); continue; } case 0xc3: { builder.Append("Ã"); continue; } case 0xc4: { builder.Append("Ä"); continue; } case 0xc5: { builder.Append("Å"); continue; } case 0xc6: { builder.Append("Æ"); continue; } case 0xc7: { builder.Append("Ç"); continue; } case 200: { builder.Append("È"); continue; } case 0xc9: { builder.Append("É"); continue; } case 0xca: { builder.Append("Ê"); continue; } case 0xcb: { builder.Append("Ë"); continue; } case 0xcc: { builder.Append("Ì"); continue; } case 0xcd: { builder.Append("Í"); continue; } case 0xce: { builder.Append("Î"); continue; } case 0xcf: { builder.Append("Ï"); continue; } case 0xd0: { builder.Append("Ð"); continue; } case 0xd1: { builder.Append("Ñ"); continue; } case 210: { builder.Append("Ò"); continue; } case 0xd3: { builder.Append("Ó"); continue; } case 0xd4: { builder.Append("Ô"); continue; } case 0xd5: { builder.Append("Õ"); continue; } case 0xd6: { builder.Append("Ö"); continue; } case 0xd7: { builder.Append("×"); continue; } case 0xd8: { builder.Append("Ø"); continue; } case 0xd9: { builder.Append("Ù"); continue; } case 0xda: { builder.Append("Ú"); continue; } case 0xdb: { builder.Append("Û"); continue; } case 220: { builder.Append("Ü"); continue; } case 0xdd: { builder.Append("Ý"); continue; } case 0xde: { builder.Append("Þ"); continue; } case 0xdf: { builder.Append("ß"); continue; } case 0xe0: { builder.Append("à"); continue; } case 0xe1: { builder.Append("á"); continue; } case 0xe2: { builder.Append("â"); continue; } case 0xe3: { builder.Append("ã"); continue; } case 0xe4: { builder.Append("ä"); continue; } case 0xe5: { builder.Append("å"); continue; } case 230: { builder.Append("æ"); continue; } case 0xe7: { builder.Append("ç"); continue; } case 0xe8: { builder.Append("è"); continue; } case 0xe9: { builder.Append("é"); continue; } case 0xea: { builder.Append("ê"); continue; } case 0xeb: { builder.Append("ë"); continue; } case 0xec: { builder.Append("ì"); continue; } case 0xed: { builder.Append("í"); continue; } case 0xee: { builder.Append("î"); continue; } case 0xef: { builder.Append("ï"); continue; } case 240: { builder.Append("ð"); continue; } case 0xf1: { builder.Append("ñ"); continue; } case 0xf2: { builder.Append("ò"); continue; } case 0xf3: { builder.Append("ó"); continue; } case 0xf4: { builder.Append("ô"); continue; } case 0xf5: { builder.Append("õ"); continue; } case 0xf6: { builder.Append("ö"); continue; } case 0xf7: { builder.Append("÷"); continue; } case 0xf8: { builder.Append("ø"); continue; } case 0xf9: { builder.Append("ù"); continue; } case 250: { builder.Append("ú"); continue; } case 0xfb: { builder.Append("û"); continue; } case 0xfc: { builder.Append("ü"); continue; } case 0xfd: { builder.Append("ý"); continue; } case 0xfe: { builder.Append("þ"); continue; } case 0xff: { builder.Append("ÿ"); continue; } case 0x152: { builder.Append("Œ"); continue; } case 0x153: { builder.Append("œ"); continue; } case 0x160: { builder.Append("Š"); continue; } case 0x161: { builder.Append("š"); continue; } case 0x178: { builder.Append("Ÿ"); continue; } case 0x192: { builder.Append("ƒ"); continue; } case 0x391: { builder.Append("Α"); continue; } case 0x392: { builder.Append("Β"); continue; } case 0x393: { builder.Append("Γ"); continue; } case 0x394: { builder.Append("Δ"); continue; } case 0x395: { builder.Append("Ε"); continue; } case 0x396: { builder.Append("Ζ"); continue; } case 0x397: { builder.Append("Η"); continue; } case 920: { builder.Append("Θ"); continue; } case 0x399: { builder.Append("Ι"); continue; } case 0x39a: { builder.Append("Κ"); continue; } case 0x39b: { builder.Append("Λ"); continue; } case 0x39c: { builder.Append("Μ"); continue; } case 0x39d: { builder.Append("Ν"); continue; } case 0x39e: { builder.Append("Ξ"); continue; } case 0x39f: { builder.Append("Ο"); continue; } case 0x3a0: { builder.Append("Π"); continue; } case 0x3a1: { builder.Append("Ρ"); continue; } case 0x3a3: { builder.Append("Σ"); continue; } case 0x3a4: { builder.Append("Τ"); continue; } case 0x3a5: { builder.Append("Υ"); continue; } case 0x3a6: { builder.Append("Φ"); continue; } case 0x3a7: { builder.Append("Χ"); continue; } case 0x3a8: { builder.Append("Ψ"); continue; } case 0x3a9: { builder.Append("Ω"); continue; } case 0x3b1: { builder.Append("α"); continue; } case 0x3b2: { builder.Append("β"); continue; } case 0x3b3: { builder.Append("γ"); continue; } case 0x3b4: { builder.Append("δ"); continue; } case 0x3b5: { builder.Append("ε"); continue; } case 950: { builder.Append("ζ"); continue; } case 0x3b7: { builder.Append("η"); continue; } case 0x3b8: { builder.Append("θ"); continue; } case 0x3b9: { builder.Append("ι"); continue; } case 0x3ba: { builder.Append("κ"); continue; } case 0x3bb: { builder.Append("λ"); continue; } case 0x3bc: { builder.Append("μ"); continue; } case 0x3bd: { builder.Append("ν"); continue; } case 0x3be: { builder.Append("ξ"); continue; } case 0x3bf: { builder.Append("ο"); continue; } case 960: { builder.Append("π"); continue; } case 0x3c1: { builder.Append("ρ"); continue; } case 0x3c2: { builder.Append("ς"); continue; } case 0x3c3: { builder.Append("σ"); continue; } case 0x3c4: { builder.Append("τ"); continue; } case 0x3c5: { builder.Append("υ"); continue; } case 0x3c6: { builder.Append("φ"); continue; } case 0x3c7: { builder.Append("χ"); continue; } case 0x3c8: { builder.Append("ψ"); continue; } case 0x3c9: { builder.Append("ω"); continue; } case 0x3d1: { builder.Append("ϑ"); continue; } case 0x3d2: { builder.Append("ϒ"); continue; } case 0x3d6: { builder.Append("ϖ"); continue; } case 0x2dc: { builder.Append("˜"); continue; } case 710: { builder.Append("ˆ"); continue; } case 0x2003: { builder.Append(" "); continue; } case 0x2009: { builder.Append(" "); continue; } case 0x200c: { builder.Append("‌"); continue; } case 0x200d: { builder.Append("‍"); continue; } case 0x200e: { builder.Append("‎"); continue; } case 0x200f: { builder.Append("‏"); continue; } case 0x2013: { builder.Append("–"); continue; } case 0x2014: { builder.Append("—"); continue; } case 0x2018: { builder.Append("‘"); continue; } case 0x2019: { builder.Append("’"); continue; } case 0x201a: { builder.Append("‚"); continue; } case 0x201c: { builder.Append("“"); continue; } case 0x201d: { builder.Append("”"); continue; } case 0x201e: { builder.Append("„"); continue; } case 0x2020: { builder.Append("†"); continue; } case 0x2021: { builder.Append("‡"); continue; } case 0x2022: { builder.Append("•"); continue; } case 0x2026: { builder.Append("…"); continue; } case 0x2030: { builder.Append("‰"); continue; } case 0x2032: { builder.Append("′"); continue; } case 0x2033: { builder.Append("″"); continue; } case 0x2039: { builder.Append("‹"); continue; } case 0x203a: { builder.Append("›"); continue; } case 0x203e: { builder.Append("‾"); continue; } case 0x2044: { builder.Append("&fras1;"); continue; } case 0x2111: { builder.Append("ℑ"); continue; } case 0x2118: { builder.Append("℘"); continue; } case 0x211c: { builder.Append("ℜ"); continue; } case 0x2122: { builder.Append("™"); continue; } case 0x2190: { builder.Append("←"); continue; } case 0x2191: { builder.Append("↑"); continue; } case 0x2192: { builder.Append("→"); continue; } case 0x2193: { builder.Append("↓"); continue; } case 0x2194: { builder.Append("↔"); continue; } case 0x2135: { builder.Append("ℵ"); continue; } case 0x21d0: { builder.Append("⇐"); continue; } case 0x21d1: { builder.Append("⇑"); continue; } case 0x21d2: { builder.Append("⇒"); continue; } case 0x21d3: { builder.Append("⇓"); continue; } case 0x21d4: { builder.Append("⇔"); continue; } case 0x21b5: { builder.Append("↵"); continue; } case 0x2200: { builder.Append("∀"); continue; } case 0x2202: { builder.Append("∂"); continue; } case 0x2203: { builder.Append("∃"); continue; } case 0x2205: { builder.Append("∅"); continue; } case 0x2207: { builder.Append("∇"); continue; } case 0x2208: { builder.Append("∈"); continue; } case 0x2209: { builder.Append("∉"); continue; } case 0x220b: { builder.Append("∋"); continue; } case 0x220f: { builder.Append("∏"); continue; } case 0x2211: { builder.Append("∑"); continue; } case 0x2212: { builder.Append("−"); continue; } case 0x221a: { builder.Append("√"); continue; } case 0x221d: { builder.Append("∝"); continue; } case 0x221e: { builder.Append("∞"); continue; } case 0x2220: { builder.Append("∠"); continue; } case 0x2217: { builder.Append("∗"); continue; } case 0x2227: { builder.Append("∧"); continue; } case 0x2228: { builder.Append("∨"); continue; } case 0x2229: { builder.Append("∩"); continue; } case 0x222a: { builder.Append("∪"); continue; } case 0x222b: { builder.Append("∫"); continue; } case 0x2234: { builder.Append("∴"); continue; } case 0x223c: { builder.Append("∼"); continue; } case 0x2245: { builder.Append("≅"); continue; } case 0x2248: { builder.Append("≈"); continue; } case 0x2260: { builder.Append("≠"); continue; } case 0x2261: { builder.Append("≡"); continue; } case 0x2264: { builder.Append("≤"); continue; } case 0x2265: { builder.Append("≥"); continue; } case 0x2282: { builder.Append("⊂"); continue; } case 0x2283: { builder.Append("⊃"); continue; } case 0x2284: { builder.Append("⊄"); continue; } case 0x2286: { builder.Append("⊆"); continue; } case 0x2287: { builder.Append("⊇"); continue; } case 0x2295: { builder.Append("⊕"); continue; } case 0x2297: { builder.Append("⊗"); continue; } case 0x2308: { builder.Append("⌈"); continue; } case 0x2309: { builder.Append("⌉"); continue; } case 0x230a: { builder.Append("⌊"); continue; } case 0x230b: { builder.Append("⌋"); continue; } case 0x22c5: { builder.Append("⋅"); continue; } case 0x22a5: { builder.Append("⊥"); continue; } case 0x2329: { builder.Append("⟨"); continue; } case 0x232a: { builder.Append("⟩"); continue; } case 0x25ca: { builder.Append("◊"); continue; } case 0x2660: { builder.Append("♠"); continue; } case 0x2663: { builder.Append("♣"); continue; } case 0x2665: { builder.Append("♥"); continue; } case 0x2666: { builder.Append("♦"); continue; } } if (i <= 0x7f) { builder.Append((char)i); } else { builder.Append("&#" + i + ";"); } } return builder.ToString(); } }