產生隨機碼

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

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)Regex 判斷是否是正負數字含小數
        /// <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

        }

 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.