using System; namespace Yes1000.Str { public class RandStr { private string framerStr = null; private string numStr = "0123456789"; private string upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private string lowerStr = "abcdefghijklmnopqrstuvwxyz"; private string markStr = @"`-=[];'\,./~!@#$%^&*()_+{}:""|<>?"; private static Random myRandom = new Random(); /// /// 如未提供参数构造,则默认由数字+小写字母构成 /// public RandStr() { framerStr = numStr + lowerStr; } /// /// 构造函数,可指定构成的字符 /// /// 是否使用数字 /// 是否使用大写字母 /// 是否使用小写字母 /// 是否使用符号 public RandStr(bool useNum,bool useUpper,bool useLower,bool useMark) { // 如果试图构造不包含任何组成字符的类,则抛出异常 if (!useNum && !useUpper && !useLower && !useMark) { throw new ArgumentException("必须至少使用一种构成字符!"); } else { if (useNum) framerStr += numStr; if (useUpper) framerStr += upperStr; if (useLower) framerStr += lowerStr; if (useMark) framerStr += markStr; } } /// /// 使用自定义的组成字符构造 /// /// 自定义字符 public RandStr(string userStr) { // 如果试图用空字符串构造类,则抛出异常 if (userStr.Length == 0) { throw new ArgumentException("请至少使用一个字符!"); } else { framerStr = userStr; } } /// /// 取得一个随机字符串 /// /// 取得随机字符串的长度 /// 返回的随机字符串 public string GetRandStr(int length) { // 获取的长度不能为0个或者负数个 if (length < 1) { throw new ArgumentException("字符长度不能为0或者负数!"); } else { // 如果只是获取少量随机字符串, // 这样没有问题. // 但如果需要短时间获取大量随机字符串的话, // 这样可能性能不高. // 可以改用StringBuilder类来提高性能, // 需要的可以自己改一下 ^o^ string tempStr = null; for (int i = 0; i < length; i++) { int randNum = myRandom.Next(framerStr.Length); tempStr += framerStr[randNum]; } return tempStr; } } } }