How to generate a random string, and specify the l

2019-02-01 17:55发布

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 specify desired length, or better, generate unique string on specification you want i.e. specify the length, a unique string within my application is enough for me.

I know I can create a Guid (Globally Unique IDentifier) but those are quite long, longer they need to be.

int length = 8;
string s = RandomString.NextRandomString(length)
uniquestringCollection = new UniquestringsCollection(length)
string s2 = uniquestringCollection.GetNext();

标签: c# string random
7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-01 18:48

You've sort of answered your own question; there is no RandomString() function because you can use a random number generator to generate a string easily.

1) Make the range of numbers between the ASCII characters that you wish to include

2) Append each character to a string until the desired length is reached.

3) Rise and repeat for each string needed (add them to an array, etc.)

Surely that'd do it?

查看更多
登录 后发表回答