【发布时间】:2020-05-03 09:11:50
【问题描述】:
我希望允许用户选择确切的长度,或者少于/多于 count 个要生成的字符(在密码生成器中)。
例如我做过:
// the longivity of the generated string
var stringChars = new char[int.Parse(TextBox2.Text)];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
// abc has been declared before, it is simply ABCD... for character generation
stringChars[i] = abc[random.Next(abc.Length)];
}
var finalString = new String(stringChars);
// this is the result box
TextBox1.Text = finalString;
现在我的问题是如果用户输入例如。 10 并且想要生成的字符串少于 10 个字符,我该怎么办?
【问题讨论】:
-
如果您允许您的用户选择字符数,为什么他想要的比他选择的要少? PS:不要那样使用
Random(),这不是一个好习惯。见:stackoverflow.com/a/19271062/12624874 -
@PhilippeB。您的链接是关于 Java 的问题,而不是 C#。
-
@Helios Lucifer 你能举个例子吗?你的问题不是很清楚...
-
@JohnathanBarclay 你是对的,我搞混了,它是固定的:)
标签: c# string for-loop passwords