在日常的工作中积累了大量的代码,这些代码能帮我们快速的完成开发工作.
这就是程序员代码积累,你可以将这些代码Ctrl + C   然后 Ctrl + V 便可以完成开发工作,效率是不是可以上去呢.?

我在这里抛块砖头,希望大家也在园子里多发一些自己积累的代码,起码方便各位同仁在工作学习的时候方便些

哈哈,如果觉得有用的话就顶顶哈~~~


using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Com.PiZiZai.FunctionControl.PiZiZaiData;


namespace Com.PiZiZai.FunctionControl.PiZiZaiString
{  
    /// <summary>
    /// 字符串操作静态类
    /// </summary>
    public static class StringOperate
    {
  
       

        #region 用正则表达式对字符串进行验证
        /// <summary>
        /// 用正则表达式对字符串进行验证
        /// </summary>
        /// <param name="str">被验证的字符串</param>
        /// <param name="i">序号:1,正整数和0;2,非负数;3,0到99之间的整数;4,中文字符;5,密码至少6个字符,可使用字母、数字及下划线!</param>
        /// <returns>返回出错语句,如为""则验正通过</returns>
        public static string InputValidate(string str, int i)
        {
            if (i == 0)
            {
                return "";
            }
            string exp = "", tip = "";
            switch (i)
            {
                case 1:
                    exp = @"^\d+$";  //正整数和0
                    tip = "只能输入正整数和0";
                    break;
                case 2:
                    exp = @"^\d+(\.\d+)?$"; //非负数
                    tip = "只能输入非负数";
                    break;
                case 3:
                    exp = @"^(\d|[1-9]\d)$";  //0到99之间的整数
                    tip = "只能输入0到99之间的整数";
                    break;
                case 4:
                    exp = @"[\u4e00-\u9fa5]"; //中文字符
                    tip = "只能输入中文字符";
                    break;
                case 5:
                    exp = @"^\w{6,}$";
                    tip = "密码至少6个字符,可使用字母、数字及下划线!";
                    break;
                default:
                    exp = "";
                    tip = "";
                    break;


            }

            if (Regex.IsMatch(str, exp))
            {
                return "";
            }
            else
            {
                return tip;
            }
        }
        #endregion

        #region 判断是否是数字
        /// <summary>
        /// 判断是否是数字
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>全数字返回True,若有某个值不为数字返回False</returns>
        public static bool IsNumeric(string str)
        {
            if (str == null || str.Length == 0)
                return false;
            System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
            byte[] bytestr = ascii.GetBytes(str);
            foreach (byte c in bytestr)
            {
                if (c < 48 || c > 57)
                {
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region 字符串直接MD5加密
        /// <summary>
        /// 字符串直接MD5加密
        /// </summary>
        /// <param name="str">需加密的字符串</param>
        /// <returns>md5加密后的密码</returns>
        public static string StringToMD5(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower();

        }
        #endregion

        #region 字符串转换小写后MD5加密
        /// <summary>
        /// 字符串转换小写后MD5加密
        /// </summary>
        /// <param name="psw">密码明码</param>
        /// <returns>md5加密后的密码</returns>
        public static string stringToMD5(string str)
        {
            str = str.ToLower();
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower();

        }
        #endregion

        #region 字符串直接SHA1加密
        /// <summary>
        /// 字符串直接SHA1加密
        /// </summary>
        /// <param name="str">需加密的字符串</param>
        /// <returns>SHA1加密后的密码</returns>
        public static string StringToSHA1(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();

        }
        #endregion

        #region 字符串转换小写后SHA1加密
        /// <summary>
        /// 字符串转换小写后SHA1加密
        /// </summary>
        /// <param name="psw">密码明码</param>
        /// <returns>SHA1加密后的密码</returns>
        public static string stringToSHA1(string str)
        {
            str = str.ToLower();
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");

        }
        #endregion

        #region 默认密钥向量 DES加密解密用
        /// <summary>
        /// 默认密钥向量
        /// </summary>
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        #endregion

        #region DES加密字符串,需设置加密密钥,要求为8位
        /// <summary>
        /// DES加密字符串,需设置加密密钥,要求为8位
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        #endregion

        #region DES解密字符串,需加密时的密钥,要求为8位
        /// <summary>
        /// DES解密字符串,需加密时的密钥,要求为8位
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
        #endregion

        #region "按当前日期和时间生成随机数"
        /// <summary>
        /// 按当前日期和时间生成随机数
        /// </summary>
        /// <param name="Num">附加随机数长度</param>
        /// <returns></returns>
        public static string sRndNum(int Num)
        {
            string sTmp_Str = System.DateTime.Today.Year.ToString() + System.DateTime.Today.Month.ToString("00") + System.DateTime.Today.Day.ToString("00") + System.DateTime.Now.Hour.ToString("00") + System.DateTime.Now.Minute.ToString("00") + System.DateTime.Now.Second.ToString("00");
            return sTmp_Str + RndNum(Num);
        }
        #endregion

        #region 生成0-9随机数
        /// <summary>
        /// 生成0-9随机数
        /// </summary>
        /// <param name="VcodeNum">生成长度</param>
        /// <returns></returns>
        public static string RndNum(int VcodeNum)
        {
            StringBuilder sb = new StringBuilder(VcodeNum);
            Random rand = new Random();
            for (int i = 1; i < VcodeNum + 1; i++)
            {
                int t = rand.Next(9);
                sb.AppendFormat("{0}", t);
            }
            return sb.ToString();

        }
        #endregion

        #region "通过RNGCryptoServiceProvider 生成随机数 0-9"
        /// <summary>
        /// 通过RNGCryptoServiceProvider 生成随机数 0-9
        /// </summary>
        /// <param name="length">随机数长度</param>
        /// <returns></returns>
        public static string RndNumRNG(int length)
        {
            byte[] bytes = new byte[16];
            RNGCryptoServiceProvider r = new RNGCryptoServiceProvider();
            StringBuilder sb = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                r.GetBytes(bytes);
                sb.AppendFormat("{0}", (int)((decimal)bytes[0] / 256 * 10));
            }
            return sb.ToString();

        }
        #endregion

        #region "转换编码"
        /// <summary>
        /// 转换编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Encode(string str)
        {
            if (str == null)
            {
                return "";
            }
            else
            {

                return System.Web.HttpUtility.UrlEncode(Encoding.GetEncoding(54936).GetBytes(str));
            }
        }
        #endregion

        #region "按字符串位数补0"
        /// <summary>
        /// 按字符串位数补0
        /// </summary>
        /// <param name="CharTxt">字符串</param>
        /// <param name="CharLen">字符长度</param>
        /// <returns></returns>
        public static string FillZero(string CharTxt, int CharLen)
        {
            if (CharTxt.Length < CharLen)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < CharLen - CharTxt.Length; i++)
                {
                    sb.Append("0");
                }
                sb.Append(CharTxt);
                return sb.ToString();
            }
            else
            {
                return CharTxt;
            }
        }

        #endregion

        #region "替换JS中特殊字符"
        /// <summary>
        /// 将JS中的特殊字符替换
        /// </summary>
        /// <param name="str">要替换字符</param>
        /// <returns></returns>
        public static string ReplaceJs(string str)
        {

            if (str != null)
            {
                str = str.Replace("\"", "&quot;");
                str = str.Replace("(", "(");
                str = str.Replace(")", ")");
                str = str.Replace("%", "%");
            }

            return str;

        }
        #endregion

        #region "正式表达式验证"
        /// <summary>
        /// 正式表达式验证
        /// </summary>
        /// <param name="C_Value">验证字符</param>
        /// <param name="C_Str">正式表达式</param>
        /// <returns>符合true不符合false</returns>
        public static bool CheckRegEx(string C_Value, string C_Str)
        {
            Regex objAlphaPatt;
            objAlphaPatt = new Regex(C_Str, RegexOptions.Compiled);


            return objAlphaPatt.Match(C_Value).Success;
        }
        #endregion

        #region "检测是否为有效邮件地址格式"
        /// <summary>
        /// 检测是否为有效邮件地址格式
        /// </summary>
        /// <param name="strIn">输入邮件地址</param>
        /// <returns></returns>
        public static bool IsValidEmail(string strIn)
        {
            return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
        #endregion

        #region "产生GUID"
        /// <summary>
        /// 获取一个GUID的HashCode
        /// </summary>
        public static int GetGUIDHashCode
        {
            get
            {
                return GetGUID.GetHashCode();
            }
        }
        /// <summary>
        /// 获取一个GUID字符串
        /// </summary>
        public static string GetGUID
        {
            get
            {
                return Guid.NewGuid().ToString();
            }
        }
        #endregion

 

    }
}

痞子再
同样,您也可以将我的代码 Ctrl + C   然后 Ctrl + V    别忘了帮我顶下啦

posted on 2008-04-18 11:33  痞子再  阅读(647)  评论(1编辑  收藏  举报