randomvalueservice.cs
来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 65 行
CS
65 行
/*
* RandomValueService.cs @Microsoft Visual Studio 2005 <.NET Framework 2.0>
* AfritXia
* 2007-12-16
*
* Copyright(c) http://www.AfritXia.NET/
*
*/
using System;
namespace NET.AfritXia.SecurityUtility
{
/// <summary>
/// 随机值服务
/// </summary>
public sealed class RandomValueService
{
// 随机数生成器
private static readonly Random g_random = new Random();
/// <summary>
/// 获取随机代码
/// </summary>
/// <param name="length">代码长度</param>
/// <param name="startWithWord">以英文字母开始</param>
/// <param name="groupSize">分组大小,当该值为 -1 时则忽略分组</param>
/// <param name="separator">分组间的间隔字符串</param>
/// <returns></returns>
public static string GetRandomString(int length, bool startWithWord, int groupSize, string separator)
{
// 随机码
string randomCode = "";
// ASCII 码
int asciiCode;
int i = 0;
while (i < length)
{
// 获取 ASCII 字符
asciiCode = g_random.Next(48, 122);
if ((asciiCode >= 58 && asciiCode <= 64) || (asciiCode >= 91 && asciiCode <= 96))
continue;
// 不以数字开头
if (startWithWord && i == 0 && (asciiCode >= 48 && asciiCode <= 57))
continue;
if (groupSize != -1)
{
if ((i != 0) && (i % Math.Abs(groupSize) == 0))
randomCode += separator;
}
randomCode += (char)asciiCode;
i++;
}
return randomCode;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?