博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#随机生成汉字、字母、数字
阅读量:4342 次
发布时间:2019-06-07

本文共 3370 字,大约阅读时间需要 11 分钟。

     ///         /// 替换变量        ///         ///         /// 
public static string replaceBianLiang(string content) { content = content.Replace("{当前时间}", DateTime.Now.TimeOfDay.ToString()); string[] bianliang = new string[] { "{随机字母}", "{随机数字}", "{随机汉字}" }; Regex r; int count; string readstr = ""; foreach (string str in bianliang) { count = (content.Length - content.Replace(str, "").Length) / str.Length; if (str == "{随机汉字}") readstr = RandChina(count); if (str == "{随机数字}") readstr = GenerateCheckCodeNum(count); if (str == "{随机字母}") readstr = GenerateRandomLetter(count); if (count > readstr.Length) count = readstr.Length; r = new Regex(str.Replace("{
", "\\{
").Replace("}", "\\}")); for (int i = 0; i < count; i++) { content = r.Replace(content, readstr.Substring(i, 1), 1); } } return content; } /// /// 随机生成字母 /// /// ///
public static string GenerateRandomLetter(int Length) { char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; string result = ""; int n = Pattern.Length; Random random = new Random(~unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < Length; i++) { int rnd = random.Next(0, n); result += Pattern[rnd]; } return result; } /// /// 随机生成数字 /// /// ///
public static string GenerateCheckCodeNum(int codeCount) { int rep = 0; string str = string.Empty; long num2 = DateTime.Now.Ticks + rep; rep++; Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep))); for (int i = 0; i < codeCount; i++) { int num = random.Next(); str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString(); } return str; } /// /// 此函数为生成指定数目的汉字 /// /// 汉字数目 ///
所有汉字
public static string RandChina(int charLen) { int area, code;//汉字由区位和码位组成(都为0-94,其中区位16-55为一级汉字区,56-87为二级汉字区,1-9为特殊字符区) StringBuilder strtem = new StringBuilder(); Random rand = new Random(); for (int i = 0; i < charLen; i++) { area = rand.Next(16, 88); if (area == 55)//第55区只有89个字符 { code = rand.Next(1, 90); } else { code = rand.Next(1, 94); } strtem.Append(Encoding.GetEncoding("GB2312").GetString(new byte[] { Convert.ToByte(area + 160), Convert.ToByte(code + 160) })); } return strtem.ToString(); }

 

转载于:https://www.cnblogs.com/vaevvaev/p/7154534.html

你可能感兴趣的文章
VsVim - Shortcut Key (快捷键)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>