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

前端 未结 11 2328
一生所求
一生所求 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条回答
  •  Happy的楠姐
    2020-12-24 07:24

    Sometimes a random string can be useful in something like a unit test, if it was me I would add the AutoFixture nuget package and then do something like this. In my example I need a 121 character string...

            var fixture = new Fixture();
            var chars = fixture.CreateMany(121).ToArray();
            var myString = new string(chars);
    

    If this wasn't in a unit test then I'd create myself a nuget package and use Autofixture in that, or something else if I needed only certain character types.

提交回复
热议问题