点击回首页
我的浏览记录 | | 帮助?
当前位置:

源码截图

源码目录树

当前路径:InsApp/word/CreateCode.cs     using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

//该源码下载自www.51aspx.com(51aspx.com)

namespace InsApp.word
{
    /// <summary>
    /// string CreateRandomCode(int codeCount)  根据长度生成随机的数字和字母
    /// bool toFilter(string thePara)           检测非法字符,如果参数是空/包含非法字符,返回false/否则返回    true
    /// bool CheckNumber(string GetNum)         判断是否是数字格式
    /// bool CheckNumberRegx(string GetNum)          判断是否是正负数字含小数
    /// bool CheckNullstr(string Getstr)        判断是否是空值null 返回true || false
    /// </summary>
    public class CreateCode
    {

        #region  生成随机的数字和字母 codeCount是希望生成的长度
        /// <summary>
        ///	生成随机的数字和字母
        /// </summary>
        /// <param name="codeCount">codeCount是希望生成的长度</param>
        /// <returns></returns>
        public string CreateRandomCode(int codeCount) //codeCount是希望生成的长度
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9";
            string[] allCharArray = allChar.Split(',');
            string randomCode = "";
            Random rand = new Random();
            int temp = -1;
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(0,10);

                temp = t;
                randomCode += allCharArray[t];
            }
           return randomCode;
       }
        #endregion

        #region  判断是否是数字格式
       /// <summary>
        /// 判断是否是数字格式
        /// </summary>
        /// <param name="GetNum"></param>
        public bool CheckNumber(string GetNum)
        {

            Regex r = new Regex(@"^[0-9]+$");
            if (r.IsMatch(GetNum))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region  检测非法字符,防止sql注入
/// <summary>
/// 检测非法字符,防止sql注入
/// 如果参数是空,返回false
/// 如果参数中包含非法字符,返回false
///// 否则返回    true
/// </summary>
/// <param name="thePara"></param>
/// <returns></returns>
        public bool toFilter(string thePara)
        {
            string[] BadCode = new string[] { "'", "\"", "exec", "cmd", ">", "<", "and", "=", "\\", ";" };
            try
            {
                if (CheckNullstr(thePara) == false)          //如果参数是空值,返回false
                {
                    throw new Exception("参数为空");
                }
                else
                {
                    for (int i = 0; i < BadCode.Length; i++)
                    {
                        if (thePara.IndexOf(BadCode[i]) > 0)
                        {
                            throw new Exception("包含非法字符");
                        }
                    }
                }
                return true;
            }
            catch 
            {
                return false;
            }


        }
#endregion

        #region  bool CheckNullstr(string Getstr)判断是否是空值
        /// <summary>
        /// Getstr得到参数判断是否是空值
        /// </summary>
        /// <param name="Getstr">需要检查的值</param>
        /// <param name="GetShow">这个字段的功能说明:姓名,sex</param>
        public bool CheckNullstr(string Getstr)
        {
            try
            {
                if (Getstr == null || Getstr == "" || Getstr.Length < 1)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch 
            {
                return false;
            }

        }
        #endregion


        #region bool CheckNumberRegx(string GetNum)正则表达式 判断是否是正负数字含小数
        /// <summary>
        /// 判断是否是数字格式
        /// </summary>
        /// <param name="GetNum"></param>
        public bool CheckNumberRegx(string GetNum)
        {
            //^[+-]?\d+(\.\d+)?$正负数字含小数     数字含小数^\d+(\.\d+)?$
            Regex r = new Regex(@"^\d+(\.\d+)?$");
            if (r.IsMatch(GetNum))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion


        #region  用C#截取指定长度的中英文混合字符串
        /// <summary>
        /// s接受的字符
        /// l长度
        /// </summary>
        /// <param name="s"></param>
        /// <param name="l"></param>
        /// <returns></returns>
        public static string CutStr(string s, int l)
        {
            string temp = s;
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
            {
                return temp;
            }
            for (int i = temp.Length; i >= 0; i--)
            {
                temp = temp.Substring(0, i);
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
                {
                    return temp + "";
                }
            }
            return "";
        }
        #endregion


        #region 数字和字母随机数
        /// <summary>
        /// 数字和字母随机数
        /// </summary>
        /// <param name="n">生成长度</param>
        /// <returns></returns>
        public static string Rand_Number_AZ_Code(int n)
        {
            char[] arrChar = new char[]{
       'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
       '0','1','2','3','4','5','6','7','8','9',
       'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
      };

            StringBuilder num = new StringBuilder();

            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());

            }

            return num.ToString();
        }
        #endregion

        #region 字母随机数
        /// <summary>
        /// 字母随机数
        /// </summary>
        /// <param name="n">生成长度</param>
        /// <returns></returns>
        public static string RandLetter(int n)
        {
            char[] arrChar = new char[]{
        'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
       'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
      };

            StringBuilder num = new StringBuilder();

            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());

            }

            return num.ToString();
        }
        #endregion

        #region 日期随机函数
        /// <summary>
        /// 日期随机函数
        /// </summary>
        /// <param name="ra">长度</param>
        /// <returns></returns>
        public static string DateRndName(Random ra)
        {
            DateTime d = DateTime.Now;
            string s = null, y, m, dd, h, mm, ss;
            y = d.Year.ToString();
            m = d.Month.ToString();
            if (m.Length < 2) m = "0" + m;
            dd = d.Day.ToString();
            if (dd.Length < 2) dd = "0" + dd;
            h = d.Hour.ToString();
            if (h.Length < 2) h = "0" + h;
            mm = d.Minute.ToString();
            if (mm.Length < 2) mm = "0" + mm;
            ss = d.Second.ToString();
            if (ss.Length < 2) ss = "0" + ss;
            s += y + m + dd + h + mm + ss;
            s += ra.Next(100, 999).ToString();
            return s;
        }
        #endregion

        #region 生成GUID
        /// <summary>
        /// 生成GUID
        /// </summary>
        /// <returns></returns>
        public static string GetGuid()
        {
            System.Guid g = System.Guid.NewGuid();
            return g.ToString();
        }
        #endregion

        }



}
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途) 企业QQ:4000410510
地址:北京市海淀区中关村鼎好大厦A座二层 邮编:100080
Room A-801,Dinghao Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2015. 京ICP备09089570号 | 京公网安备11010702000869号
在线客服
分享该页面
关闭侧边栏