.NET(C#)生成指定長度的隨機字符串的通用方法,此方法可以指定字符串的長度,是否包含數字,是否包含符號,是否包含小寫字母,是否包含大寫字母等,
源碼:
1 #region 生成指定長度的隨機字符串 2 /// <summary> 3 /// 生成指定長度的隨機字符串 4 /// </summary> 5 /// <param name="intLength">隨機字符串長度</param> 6 /// <param name="booNumber">生成的字符串中是否包含數字</param> 7 /// <param name="booSign">生成的字符串中是否包含符號</param> 8 /// <param name="booSmallword">生成的字符串中是否包含小寫字母</param> 9 /// <param name="booBigword">生成的字符串中是否包含大寫字母</param> 10 /// <returns></returns> 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 < intLength) 20 { 21 //生成隨機數A,表示生成類型 22 //1=數字,2=符號,3=小寫字母,4=大寫字母 23 24 intA = ranA.Next(1, 5); 25 26 //如果隨機數A=1,則運行生成數字 27 //生成隨機數A,范圍在0-10 28 //把隨機數A,轉成字符 29 //生成完,位數+1,字符串累加,結束本次循環 30 31 if (intA == 1 && booNumber) 32 { 33 intA = ranA.Next(0, 10); 34 strB = intA.ToString() + strB; 35 intResultRound = intResultRound + 1; 36 continue; 37 } 38 39 //如果隨機數A=2,則運行生成符號 40 //生成隨機數A,表示生成值域 41 //1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域 42 43 if (intA == 2 && booSign == true) 44 { 45 intA = ranA.Next(1, 5); 46 47 //如果A=1 48 //生成隨機數A,33-47的Ascii碼 49 //把隨機數A,轉成字符 50 //生成完,位數+1,字符串累加,結束本次循環 51 52 if (intA == 1) 53 { 54 intA = ranA.Next(33, 48); 55 strB = ((char)intA).ToString() + strB; 56 intResultRound = intResultRound + 1; 57 continue; 58 } 59 60 //如果A=2 61 //生成隨機數A,58-64的Ascii碼 62 //把隨機數A,轉成字符 63 //生成完,位數+1,字符串累加,結束本次循環 64 65 if (intA == 2) 66 { 67 intA = ranA.Next(58, 65); 68 strB = ((char)intA).ToString() + strB; 69 intResultRound = intResultRound + 1; 70 continue; 71 } 72 73 //如果A=3 74 //生成隨機數A,91-96的Ascii碼 75 //把隨機數A,轉成字符 76 //生成完,位數+1,字符串累加,結束本次循環 77 78 if (intA == 3) 79 { 80 intA = ranA.Next(91, 97); 81 strB = ((char)intA).ToString() + strB; 82 intResultRound = intResultRound + 1; 83 continue; 84 } 85 86 //如果A=4 87 //生成隨機數A,123-126的Ascii碼 88 //把隨機數A,轉成字符 89 //生成完,位數+1,字符串累加,結束本次循環 90 91 if (intA == 4) 92 { 93 intA = ranA.Next(123, 127); 94 strB = ((char)intA).ToString() + strB; 95 intResultRound = intResultRound + 1; 96 continue; 97 } 98 99 } 100 101 //如果隨機數A=3,則運行生成小寫字母 102 //生成隨機數A,范圍在97-122 103 //把隨機數A,轉成字符 104 //生成完,位數+1,字符串累加,結束本次循環 105 106 if (intA == 3 && booSmallword == true) 107 { 108 intA = ranA.Next(97, 123); 109 strB = ((char)intA).ToString() + strB; 110 intResultRound = intResultRound + 1; 111 continue; 112 } 113 114 //如果隨機數A=4,則運行生成大寫字母 115 //生成隨機數A,范圍在65-90 116 //把隨機數A,轉成字符 117 //生成完,位數+1,字符串累加,結束本次循環 118 119 if (intA == 4 && booBigword == true) 120 { 121 intA = ranA.Next(65, 89); 122 strB = ((char)intA).ToString() + strB; 123 intResultRound = intResultRound + 1; 124 continue; 125 } 126 } 127 return strB; 128 129 } 130 #endregion
讀取數據:
1 private void button1_Click(object sender, EventArgs e) 2 { 3 foreach (var file in Directory.GetFiles("E:\\renew", "*.txt").Where(t => 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<string> list = new List<string>(); 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 }