How to generate a random string, and specify the length you want, or better generate unique string on specification you want

前端 未结 11 2336
一生所求
一生所求 2020-12-24 06:24

There is a library to generate Random numbers, so why isn\'t there a library for generation of random strings?

In other words how to generate a random string, and sp

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 07:10

    Here is some modification from above answers.

      private static string GetFixedLengthStrinng(int len)
        {
            const string possibleAllChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!@#$%^&*()_+{}:',.?/";
            StringBuilder sb = new StringBuilder();
            Random randomNumber = new Random();
            for (int i = 0; i < len; i++)
            {
                sb.Append(possibleAllChars[randomNumber.Next(0, possibleAllChars.Length)]);
            }
            return sb.ToString();
        }
    

提交回复
热议问题