C# 生成随机的字符代码
代码如下:
/// <summary>
/// 生成随机码
/// </summary>
public class CheckCode
{
private string strType = "";//字符类型
/// <summary>
/// 生成随机码
/// </summary>
/// <param name="digital">数字</param>
/// <param name="character">字符</param>
/// <param name="symbol">符号</param>
public CheckCode(bool digital, bool character, bool symbol)
{
//数字
if (digital)
{
strType = "1|2|3|4|5|6|7|8|9|0";
}
//字符
if (character)
{
if (strType != "") strType += "|";//避免前面不选
strType += "q|w|e|r|t|y|u|i|o|p|a|s|d|f|g|h|j|k|l|z|x|c|v|b|n|m";
}
//符号
if (symbol)
{
if (strType != "") strType += "|";//避免前面不选
strType += ",|.|/|;|'|[|]|{|}|;|<|>|?|!|@|#|$|%|^|&|*|(|)|_|-|+|=|~";
}
}
/// <summary>
/// 获取随机码
/// </summary>
/// <param name="Length">长度</param>
/// <returns></returns>
public string GoRandom(int Length)
{
string strRandom = "";
string[] zf = strType.Split('|');
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
int sjs = rd.Next(zf.Length);
strRandom += zf[sjs];
}
return strRandom;
}
}
使用方法:
方法A:
CheckCode checkCode = new CheckCode(true,true,false);
string code = checkCode.GoRandom(10);
方法B:
string code = new CheckCode(true,true,false).GoRandom(10);
关联 JAVA 版的随机类:JAVA 版
附件下载:CheckCode.cs
版权属于:zgcwkj
本文链接:https://blog.zgcwkj.cn/archives/29.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭