#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. js数组的各种方法

    1.检测数组 ①Instanceof: if(value instanceof Array){ } 它假定只有一个全局执行环境,若网页中包含多个框架,则存在多个不同的全局执行环境,则Instanceo ...

  2. window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory

    今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpre ...

  3. UIPageViewController 翻页、新手引导--UIScrollView:pagingEnabled

    UIPageViewController 翻页.新手引导--UIScrollView:pagingEnabled

  4. Flask框架 之第一个Flask程序

    from flask import Flask # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录为总目录,static目录为静态目录,templates为模 ...

  5. vue(数据改变,DOM不渲染问题)

    1.组件内部,属性值地址空间内引用地址改变,DOM不能渲染. 问题举例:this.items = [[],[],[],[]] 1.在items 中,修改任意一项数组中的值,DOM是不会更新的,2.解决 ...

  6. 解决header,footer等HTML5标签在IE(IE6/IE7/IE8)无效的方法

    HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. HTML5的新标签元素有: <header&g ...

  7. 完善本地搭建的jekyll环境(Windows)

    序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来.中文编码有问题.这说明之前搭建的环境有不周之处. PS:因之前自己搭建环境时并未 ...

  8. 当ECharts碰到TWaver

    百度公司的ECharts发展迅速,已经成为HTML5 Chart的佼佼者,这让大家骄傲:中国人终于也有世界级的开源通用UI产品了.正如其网站所说,它是百度的,是中国的,也是世界的.想想那些年,我们追逐 ...

  9. rem js 自适应布局

    (function(doc, win) { resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',//o ...

  10. springboot 生产环境不能访问swagger

    @Profile({"local", "dev", "test"}) local,dev, test 分支都可以访问swagger