当前位置: 编程技术>.net/c#/asp.net
c#固定长度的随机字符串例子
来源: 互联网 发布时间:2014-10-18
本文导语: 代码如下:/// /// 固定长度的随机字符串/// /// 长度/// 随机串public static String getRandStringEx(int length){char[] charList = {'0','1','2','3','4','5','6','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','a','b'...
代码如下:
///
/// 固定长度的随机字符串
///
/// 长度
/// 随机串
public static String getRandStringEx(int length)
{
char[] charList = {'0','1','2','3','4','5','6','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',
'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[] rev = new char[length];
Random f = new Random();
for (int i = 0; i < length; i++)
{
rev[i] = charList[Math.Abs(f.Next(127)) % length];
}
return new String(rev);
}