我有一个简短的问题。我有一个注册机来为我的应用程序生成随 secret 码。它生成大写字母和数字,但我希望它像某些程序一样格式化它们的代码,如 xxxx-xxxx-xxxx
。到目前为止我的代码是这样的
Random random = new Random(0);
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = getrandomcode();
}
public string getrandomcode()
{
char[] tokens = {'0', '1', '2', '3', '4', '5', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] codeArray = new char[24];
for (int i = 0; i < 24; i++)
{
int index = random.Next(tokens.Length - 1);
codeArray[i] = tokens[index];
}
return new String(codeArray);
}
它简单而不复杂,所以我希望有一种方法可以实现此代码的“-”。
提前致谢!
最佳答案
将它包含在你的“for”循环中:
if (i % 5 == 4)
{
codeArray[i] = '-';
}
else
{
int index = random.Next(tokens.Length - 1);
codeArray[i] = tokens[index];
}
关于c# - 如何制作一个在 C# 中每四个字母写入 "-"的注册机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935260/