[C#]NetxtString_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > [C#]NetxtString

[C#]NetxtString

 2014/8/11 12:51:51  楚人游子  程序员俱乐部  我要评论(0)
  • 摘要:关键代码:///<summary>///生成随机字符串///</summary>///<paramname="random">Random</param>///<paramname="size">字符串长度</param>///<paramname="lowerCase">字符串是小写</param>///<returns>随机字符串</returns>
  • 标签:C# net

关键代码:

class="csharpcode">        /// <summary>
        /// 生成随机字符串
        /// </summary>
        /// <param name="random">Random</param>
        /// <param name="size">字符串长度</param>
        /// <param name="lowerCase">字符串是小写</param>
        /// <returns>随机字符串</returns>
        public static string NetxtString(this Random random, int size, bool lowerCase)
        {
            StringBuilder _builder = new StringBuilder(size);
            int _startChar = lowerCase ? 97 : 65;//65 = A / 97 = a
            for (int i = 0; i < size; i++)
                _builder.Append((char)(26 * random.NextDouble() + _startChar));
            return _builder.ToString();
        }

测试代码:

        static void Main(string[] args)
        {
            try
            {
                Random _random = new Random();
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(_random.NetxtString(4, false));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }

测试结果:

image

 

二,关键代码:

        /// <summary>
        /// 0~9 A~Z字符串
        /// </summary>
        public static string RandomString_09AZ = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ";
        /// <summary>
        /// 依据指定字符串来生成随机字符串
        /// </summary>
        /// <param name="random">Random</param>
        /// <param name="randomString">指定字符串</param>
        /// <param name="size">字符串长度</param>
        /// <param name="lowerCase">字符串是小写</param>
        /// <returns>随机字符串</returns>
        public static string NetxtString(this Random random, string randomString, int size, bool lowerCase)
        {
            string _nextString = string.Empty;
            if (random != null && !string.IsNullOrEmpty(randomString))
            {
                StringBuilder _builder = new StringBuilder(size);
                int _maxCount = randomString.Length - 1;
                for (int i = 0; i < size; i++)
                {
                    int _number = random.Next(0, _maxCount);
                    _builder.Append(randomString[_number]);
                }
                _nextString = _builder.ToString();
            }
            return lowerCase ? _nextString.ToLower() : _nextString.ToUpper();
        }

测试代码:

        static void Main(string[] args)
        {
            try
            {
                Random _random = new Random();
                string _ramdomString = RandomToolV2.RandomString_09AZ;
                for (int i = 0; i < 20; i++)
                {
                    // Console.WriteLine(_random.NetxtString(4, false));
                    Console.WriteLine(_random.NetxtString(_ramdomString, 4, false));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }

代码效果:

image

希望有所帮助!

发表评论
用户名: 匿名