Encode Javascript - CSharp System

CSharp examples for System:String Encode Decode

Description

Encode Javascript

Demo Code

/*****************************************************************
 * Copyright (C) 2005-2006 Newegg Corporation
 * All rights reserved.// ww  w.  ja v  a2 s  . 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{
        private static string EncodeJs(string strInput)
      {
         if (strInput == null)
         {
            return null;
         }
         if (strInput.Length == 0)
         {
            return "\"\"";
         }
         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 if (ch > '\x007f')
            {
               builder.Append(@"\u" + TwoByteHex(ch));
            }
            else
            {
               builder.Append(@"\x" + SingleByteHex(ch));
            }
         }
         builder.Append("\"");
         return builder.ToString();
      }
}

Related Tutorials