(C#)生成指定长度的随机字符串的通用方法 您所在的位置:网站首页 随机生成指定长度字符串 (C#)生成指定长度的随机字符串的通用方法

(C#)生成指定长度的随机字符串的通用方法

2024-06-30 22:25| 来源: 网络整理| 查看: 265

.NET(C#)生成指定长度的随机字符串的通用方法,此方法可以指定字符串的长度,是否包含数字,是否包含符号,是否包含小写字母,是否包含大写字母等,

源码:

1 #region 生成指定长度的随机字符串 2 /// 3 /// 生成指定长度的随机字符串 4 /// 5 /// 随机字符串长度 6 /// 生成的字符串中是否包含数字 7 /// 生成的字符串中是否包含符号 8 /// 生成的字符串中是否包含小写字母 9 /// 生成的字符串中是否包含大写字母 10 /// 11 public string GetRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword) 12 { 13 //定义 14 Random ranA = new Random(); 15 int intResultRound = 0; 16 int intA = 0; 17 string strB = ""; 18 19 while (intResultRound t.ToLower().EndsWith(".txt")).ToList()) 4 { 5 using (StreamReader reader = new StreamReader(file, Encoding.UTF8)) 6 { 7 string strLine = string.Empty; 8 while ((strLine = reader.ReadLine()) != null) 9 { 10 renewModel.Content = strLine; 11 renewBLL.Add(renewModel); 12 } 13 } 14 } 15 }

写入数据:

1 private void button2_Click(object sender, EventArgs e) 2 { 3 StreamWriter sw = new StreamWriter(@"E:\test.txt", true, Encoding.UTF8); 4 List list = new List(); 5 while (1 == 1) 6 { 7 string str = "CCKD"; 8 str += GetRandomizer(5, true, false, false, true); 9 if (!list.Contains(str)) 10 { 11 list.Add(str); 12 sw.WriteLine(str); 13 sw.Flush(); 14 } 15 if (list.Count== 100000) 16 break; 17 } 18 sw.Close(); 19 }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有