Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0664 Escaping Unicode characters for HTML

using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string htmlFragment = "this is a test"; string result = Regex.Replace (htmlFragment, @"[\u0080-\uFFFF]", m => @"&#" + ((int)m.Value[0]).ToString() + ";"); Console.WriteLine (result); } } The output: this is a test