C# · 12月 21, 2021

C#生成随机字符串的实例

复制代码 代码如下:
/// <summary>
/// 生成随机字符串
/// </summary>
private class RandomStringGenerator
{
static readonly Random r = new Random();
const string _chars = “0123456789”;
public static string GetRandomString()
{
char[] buffer = new char[5];
for (int i = 0; i < 5; i++)
{
buffer[i] = _chars[r.Next(_chars.Length)];
}
return new string(buffer);
}
}