c# .Net随机生成字符串代码

时间:2022-07-01 15:12:04
/// <summary>
/// 随机生成字符串
/// </summary>
/// <param name="OperationType">数字字母组合:NUMBERLETTER 数字:NUMBER 大小写组合字母:ALLLETTER 大写字母:UPLETTER 小写字母:LOWLETTER</param>
/// <param name="Length">位数</param>
/// <returns></returns>
public static string CharM(string OperationType, int Length)
{
string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
switch (OperationType)
{
case "NUMBERLETTER"://全字符模式
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
break;
case "NUMBER"://数字模式
chars = "";
break;
case "ALLLETTER"://大小写字母模式
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
break;
case "UPLETTER"://大写字母模式
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
case "LOWLETTER"://小写字母模式
chars = "abcdefghijklmnopqrstuvwxyz";
break;
}
Random randrom = new Random((int)DateTime.Now.Ticks); string str = "";
for (int i = ; i < Length; i++)
{
str += chars[randrom.Next(chars.Length)];
}
return str;
}

生成随机字符串,字符组合等...