Encode Html - CSharp System

CSharp examples for System:String HTML

Description

Encode Html

Demo Code

/*****************************************************************
 * Copyright (C) 2005-2006 Newegg Corporation
 * All rights reserved./*from w  w  w . ja  v a  2s.  c  o  m*/
 * 
 * Author:   Jason Huang (jaosn.j.huang@newegg.com)
 * Create Date:  07/02/2008 15:12:41
 * Usage:
 *
 * RevisionHistory
 * Date         Author               Description
 * 
*****************************************************************/
using System.Text;

public class Main{
        // Methods
      private static string EncodeHtml(string strInput)
      {
         if (strInput == null)
         {
            return null;
         }
         if (strInput.Length == 0)
         {
            return string.Empty;
         }
         StringBuilder builder = new StringBuilder("", strInput.Length * 2);
         foreach (char ch in strInput)
         {
            if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch == ' ') || ((ch > '/') && (ch < ':'))) || (((ch == '.') || (ch == ',')) || ((ch == '-') || (ch == '_')))))
            {
               builder.Append(ch);
            }
            else
            {
               builder.Append("&#" + ((int)ch).ToString() + ";");
            }
         }
         return builder.ToString();
      }
}

Related Tutorials