[C #] NetxtString,

Source: Internet
Author: User

[C #] NetxtString,

Key code:

/// <Summary> /// generate a random string /// </summary> /// <param name = "Random"> random </param> // <param name = "size"> String Length </param> /// <param name = "lowerCase"> the string is a lowerCase </param> /// <returns> random string </ 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 ();}

Test code:

        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();            }        }

Test results:

/// <Summary> /// 0 ~ 9 ~ Z string // </summary> public static string RandomString_09AZ = "0123456789 ABCDEFGHIJKMLNOPQRSTUVWXYZ "; /// <summary> /// generate a random string based on the specified string /// </summary> /// <param name = "Random"> random </param>/ // <param name = "randomString"> specify the string </param> /// <param name = "size"> String Length </param> /// <param name = "lowerCase"> the string is a lowerCase </param> // <returns> Random string </returns> public static string NetxtString (this random Random, stri Ng 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 ();}

Test code:

        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();            }        }

Code effect:

Hope this is helpful!


What is PC

Personal computer, personal computer =

What is the PC end?

The PC end is the interface for accessing the personal computer. Some mobile phones should prompt pc capacity storage when accessing the computer, which means to access the personal computer. A pc is a personal computer. Permissioncomputer (PC) generally refers to terminals, user terminals, and clients.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.