C#根据规则生成6位随机码
#region 获得6位优惠码 zhy
public static string CreatePromoCode(string code)
{
if (code == "")
{
return "a00001";
}
else
{
string new_code = "";
new_code = CreateGrapheme(code);
new_code += CreateNumber(code);
return new_code;
}
}
#endregion #region 拼接字母 zhy
public static string CreateGrapheme(string code)
{
string new_code = "";
//获取字母
string grapheme = GetGrapheme(code);
//获得数字
string num = GetNumber(code);
//最后一个字母为z从新拼接否则获取该字母下一个字母拼接
if (grapheme.Substring(grapheme.Length - ) == "Z")
{
switch (grapheme.Length)
{
case :
if (Convert.ToInt32(num) >= )
{
new_code = "AA";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
new_code = "AAA";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
new_code = "AAAA";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
new_code = "AAAAA";
}
else
{
new_code += grapheme;
}
break;
}
}
else
{
switch (grapheme.Length)
{
case :
if (Convert.ToInt32(num) >= )
{
char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - ).ToCharArray()[]) + );
new_code += grapheme.Substring(, new_code.Length - ) + new_grapheme.ToString();
num = "";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - ).ToCharArray()[]) + );
new_code += grapheme.Substring(, new_code.Length - ) + new_grapheme.ToString();
num = "";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - ).ToCharArray()[]) + );
new_code += grapheme.Substring(, new_code.Length - ) + new_grapheme.ToString();
num = "";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - ).ToCharArray()[]) + );
new_code += grapheme.Substring(, new_code.Length - ) + new_grapheme.ToString();
num = "";
}
else
{
new_code += grapheme;
}
break;
case :
if (Convert.ToInt32(num) >= )
{
char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - ).ToCharArray()[]) + );
new_code += grapheme.Substring(, new_code.Length - ) + new_grapheme.ToString();
num = "";
}
else
{
new_code += grapheme;
}
break;
}
}
return new_code;
}
#endregion #region 获得字母 zhy
public static string GetGrapheme(string code)
{
//定义获取字母的正则
Regex grapheme_regex = new Regex(@"[A-Z]+");
//找到字符串中的匹配项
Match grapheme_match = grapheme_regex.Match(code);
return grapheme_match.Value;
}
#endregion #region 获得数字 zhy
public static string GetNumber(string code)
{
//定义获取数字的正则
Regex num_regex = new Regex(@"[^\d.\d]");
//剔除字符串中除数字以外的字符
string num = Regex.Replace(code, @"[^\d.\d]", "").ToString();
return num;
}
#endregion #region 拼接数字 zhy
public static string CreateNumber(string code)
{
string new_code = "";
//获取字母
string grapheme = GetGrapheme(code);
//获得数字
string num = GetNumber(code); int old_num = Convert.ToInt32(num);
//计算字母后的数字
switch (num.ToString().Length)
{
case :
if (old_num < )
{
new_code = new_code + (old_num++);
}
else
{
//优惠码已经配到头
new_code = "";
}
break;
case :
if (old_num < )
{
int temporary_num = old_num;
temporary_num++;
//判断数字是否从01开始
if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else
{
new_code = new_code + temporary_num.ToString();
}
}
else
{
//优惠码已经配到头
new_code = new_code + "";
}
break;
case :
if (old_num < )
{
int temporary_num = old_num;
temporary_num++;
//判断数字是否从01开始
if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else
{
new_code = new_code + temporary_num.ToString();
}
}
else
{
//优惠码已经配到头
new_code = "";
}
break;
case :
if (old_num < )
{
int temporary_num = old_num;
temporary_num++;
//判断数字是否从01开始
if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else
{
new_code = new_code + temporary_num.ToString();
}
}
else
{
//优惠码已经配到头
new_code = "";
}
break;
case :
if (old_num < )
{
int temporary_num = old_num;
temporary_num++;
//判断数字是否从01开始
if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else if (temporary_num.ToString().Length == )
{
new_code = new_code + "" + temporary_num.ToString();
}
else
{
new_code = new_code + temporary_num.ToString();
}
}
else
{
//优惠码已经配到头
new_code = "";
}
break;
}
return new_code;
}
#endregion
C#根据规则生成6位随机码的更多相关文章
- js生成6位随机码
js生成6位随机数字: let chars = '0123456789'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ let maxPos = chars.len ...
- 生成一个字母数字组合的n位随机码、随机数、随机字符串
package com.cms.util; /** * 生成一个字母数字组合的n位随机码 * @author abc * */ public class CodeUtil { // private f ...
- JAVA 生成无重复8位随机码(转)
转载自:https://my.oschina.net/vvcumt/blog/491504 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个. 本 ...
- android中执行(定时任务)的方法及6位随机码的产生
在网上看了很多类似的文章,比较乱,自己总结了一下,在开发中,常见的执行定时任务的方法有以下几种, 很简单的描述,有什么不懂可以留言,下面来介绍一下这几种常见的方法: 1.直接在线程中睡觉的方法,这个比 ...
- js 生成m位随机数入门实例
1.根据时间生成m位随机数,最大13位随机数,并且不能保证首位不为0. 例子: function ran(m) { m = m > 13 ? 13 : m; var num = new Date ...
- java 生成8位数字作为UID
java 生成8位数字作为UUID: /*** * 生成uid 8位数字 */public static String generateUID(){ Random random = new Rando ...
- 生成24位字符串ID__IdGenerator.java
此工具类用于生成24位字符串ID,唯一不重复.直接通过 IdGenerator.get() 获取. 源码如下:(点击下载源码 - IdGenerator.java ) import java.net. ...
- 在使用Math.random()生成6位随机数遇到的问题,并成功得到6位随机数
最近在做卫生局的一个考务网时需要实现一个短信发送验证码的功能,因此就必须使用到随机生成6位验证码的功能,开始觉的简单的,随便写了个 +); String messageCode = String.va ...
- 生成64位代码的mdb数据库连接串Provider的设置
生成32位程序的mdb连接串的 Provider为:Provider=Microsoft.Jet.OLEDB.4.0 而生成64位代码时,则需要使用如下的 Provider Provider=Micr ...
随机推荐
- IDEA 提示Cannot resolve symbol
将Springboot项目,构建为多个模块的时候,idea的有些类中报错Cannot resolve symbol 解决: 1.File->Invalidate Caches/Restart 清 ...
- Linux系统调用--getrusage函数详解
Linux系统调用--getrusage函数详解 功能描述: 获得进程的相关资源信息.如:用户开销时间,系统开销时间,接收的信号量等等; 用法: #include <sys/t ...
- c++写入txt
用ofstream 输出流,#include <fstream> ofstream outf; outf.open("abc.txt");outf<<123 ...
- oracle关闭
Alert log 要每天查看 abort 关闭冷备会无法使用
- 洛谷——P1342 请柬
P1342 请柬 题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计划.许多学 ...
- 魂酥的LNOI2019滚粗记
$Day -???$ 高一下终于开始了 在开学文化课考试的水题之下混了个(成绩)前排 于是我便油然而生一种自信 我!要!进!省!队! 讲句真话我这么想的时候连自己都觉得自己是个十足的沙雕 我又不是zw ...
- layui 动态表格之合并单元格
需求: 下面用excel表格大概模拟下需求,左边是原来的,要改成右边这样的: ①第一步:再生成表格后调用此方法,以合并重复的单元格 done : function(res, curr, count) ...
- Datatable 插入一行数据到第一行
var t = $('#passwdHOST').DataTable({ 'searching': true, 'ordering': false, 'autoWidth': false, dom: ...
- wake states
Wake states Table of Contents 1. Turbulent Wake 2. turbulent wake of a circular cylinder 3. Referenc ...
- Leetcode 99.恢复二叉搜索树
恢复二叉搜索树 二叉搜索树中的两个节点被错误地交换. 请在不改变其结构的情况下,恢复这棵树. 示例 1: 输入: [1,3,null,null,2] 输出: [3,1,null,null,2] 示例 ...