c#生成随机字符串_string合并字符串

(2) 2024-07-12 18:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
c#生成随机字符串_string合并字符串,希望能够帮助你!!!。

// c#产生随机字符串的两种方法 using System; using System.Data; using System.Linq; using System.Windows.Forms; namespace 随机产生字符串 { 
    public partial class Form1 : Form { 
    public Form1() { 
    InitializeComponent(); } Random random = new Random(); private string chars = "ABCDEFGHIJKLMNOPQRSTUWVXYZ0abcdefghijklmnopqrstuvwxyz"; private void button1_Click(object sender, EventArgs e) { 
    this.textBox1.Clear(); this.textBox1.AppendText(radomstrs(chars, 8)); } /// <summary> /// 随机字符方法一 遍历返回 /// </summary> /// <param name="chars">随机字符串源</param> /// <param name="length">返回随机的字符串个数</param> /// <returns></returns> private string radomstrs(string chars ,int length) { 
    string strs = string.Empty; for (int i = 0; i < length; i++) { 
    strs += chars[random.Next(chars.Length)]; } return strs; } private void button2_Click(object sender, EventArgs e) { 
    this.textBox1.Clear(); this.textBox1.AppendText(radomstrsbyling(chars, 8)); } /// <summary> /// 随机字符方法二 System.Linq.Enumerable; /// </summary> /// <param name="chars">随机字符串源</param> /// <param name="length">返回随机的字符串个数</param> /// <returns></returns> private string radomstrsbyling(string chars, int length) { 
    return new string( Enumerable.Repeat(chars, length).Select(s => s[random.Next(chars.Length)]).ToArray()); } } } 
Random random = new Random(); byte[] bytes = new byte[random.Next(0, 10000)]; System.Security.Cryptography.RNGCryptoServiceProvider rNGCryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider(); rNGCryptoServiceProvider.GetBytes(bytes); int num = BitConverter.ToInt32(bytes, 0); 
 private static Random random = new Random(); /// <summary> /// 随机字符串 /// </summary> /// <param name="chars"></param> /// <param name="length"></param> /// <returns></returns> public string GetRandomStr(string chars, int length) { 
    if (string.IsNullOrEmpty(chars)) { 
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghizklmnopqrstuvwxyz0"; } //const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; return new string(Enumerable.Repeat(chars, length) .Select(s => s[random.Next(s.Length)]).ToArray()); } 

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复