VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之c# .Net随机生成字符串代码

/// <summary>
        /// 随机生成字符串
        /// </summary>
        /// <param name="OperationType">数字字母组合:NUMBERLETTER 数字:NUMBER  大小写组合字母:ALLLETTER 大写字母:UPLETTER 小写字母:LOWLETTER</param>
        /// <param name="Length">位数</param>
        /// <returns></returns>
        public static string CharM(string OperationType, int Length)
        {
            string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            switch (OperationType)
            {
                case "NUMBERLETTER"://全字符模式
                    chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    break;
                case "NUMBER"://数字模式
                    chars = "0123456789";
                    break;
                case "ALLLETTER"://大小写字母模式
                    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                    break;
                case "UPLETTER"://大写字母模式
                    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    break;
                case "LOWLETTER"://小写字母模式
                    chars = "abcdefghijklmnopqrstuvwxyz";
                    break;             
            }
            Random randrom = new Random((int)DateTime.Now.Ticks);

            string str = "";
            for (int i = 0; i < Length; i++)
            {
                str += chars[randrom.Next(chars.Length)];
            }          
            return str;
        }
复制代码

生成随机字符串,字符组合等...


相关教程