#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位随机码的更多相关文章

  1. js生成6位随机码

    js生成6位随机数字: let chars = '0123456789'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ let maxPos = chars.len ...

  2. 生成一个字母数字组合的n位随机码、随机数、随机字符串

    package com.cms.util; /** * 生成一个字母数字组合的n位随机码 * @author abc * */ public class CodeUtil { // private f ...

  3. JAVA 生成无重复8位随机码(转)

    转载自:https://my.oschina.net/vvcumt/blog/491504 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个. 本 ...

  4. android中执行(定时任务)的方法及6位随机码的产生

    在网上看了很多类似的文章,比较乱,自己总结了一下,在开发中,常见的执行定时任务的方法有以下几种, 很简单的描述,有什么不懂可以留言,下面来介绍一下这几种常见的方法: 1.直接在线程中睡觉的方法,这个比 ...

  5. js 生成m位随机数入门实例

    1.根据时间生成m位随机数,最大13位随机数,并且不能保证首位不为0. 例子: function ran(m) { m = m > 13 ? 13 : m; var num = new Date ...

  6. java 生成8位数字作为UID

    java 生成8位数字作为UUID: /*** * 生成uid 8位数字 */public static String generateUID(){ Random random = new Rando ...

  7. 生成24位字符串ID__IdGenerator.java

    此工具类用于生成24位字符串ID,唯一不重复.直接通过 IdGenerator.get() 获取. 源码如下:(点击下载源码 - IdGenerator.java ) import java.net. ...

  8. 在使用Math.random()生成6位随机数遇到的问题,并成功得到6位随机数

    最近在做卫生局的一个考务网时需要实现一个短信发送验证码的功能,因此就必须使用到随机生成6位验证码的功能,开始觉的简单的,随便写了个 +); String messageCode = String.va ...

  9. 生成64位代码的mdb数据库连接串Provider的设置

    生成32位程序的mdb连接串的 Provider为:Provider=Microsoft.Jet.OLEDB.4.0 而生成64位代码时,则需要使用如下的 Provider Provider=Micr ...

随机推荐

  1. Win7 下 PB (PowerBuilder) Insert Control 崩溃的解决办法

    环境: WIN7 x86  PB8.0, x64系统目录不同,不过也可以试试 Insert -> OLE... -> Insert Control  - 崩溃 如果网上提供的办法解决不了你 ...

  2. 【sqli-labs】 对于less34 less36的宽字节注入的一点深入

    1.AddSlashes() 首先来观察一下是如何通过构造吃掉转义字符的 先将less 34的网页编码换成gbk 加上一些输出 echo "Before addslashes(): &quo ...

  3. CAD绘制一个单行文字(com接口VB语言)

    主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX >文字的位置的X坐标 DOUBLE dPosY 文字的位置的Y ...

  4. @ExceptionHandler和@ControllerAdvice统一处理异常

    //@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...

  5. java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS

    解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...

  6. 服务器做ssh免秘钥登陆

    集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...

  7. db2构建临时结果集

    一 values  ('1',2,3)   为一行   ‘1’   2    3   行数据类型可以不同  values  ('1',2,3),('f',5,6) 为两行 (values  1,2,3 ...

  8. Vue2 + Koa2 实现后台管理系统

    看了些 koa2 与 Vue2 的资料,模仿着做了一个基本的后台管理系统,包括增.删.改.查与图片上传. 工程目录: 由于 koa2 用到了 async await 语法,所以 node 的版本需要至 ...

  9. CCF201512-2 消除类游戏 java(100分)

    试题编号: 201512-2 试题名称: 消除类游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进 ...

  10. LINUX-用户和群组

    groupadd group_name 创建一个新用户组 groupdel group_name 删除一个用户组 groupmod -n new_group_name old_group_name 重 ...