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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.