#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. Android 百度地图 android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapView

    android.view.InflateException: Binary XML file line Error inflating class com.baidu.mapapi.map.MapVi ...

  2. UUID 生成32位随机串

    java通过jdk自带的UUID,生成32位的随机串 private static String generate_UUID() { UUID uuid=UUID.randomUUID(); Stri ...

  3. PHP——基本使用(二)

    PHP与Apache Apache服务器在接受到客户端请求的时候,根据客户端所请求的文件的类型,然后去问模块能否处理此文件,php作为模块之一有可能可以处理此文件,处理之后将数据再返回给apache, ...

  4. 00Extensible Markup Language

    Extensible Markup Language XML(Extensible Markup Language)可扩展标记语言是用来网络数据的组织结构,传输及存储.

  5. 13EL表达式语言

    EL表达式语言 EL表达式语言 JSP用于在页面上显示动态内容,通常需要在JSP页面中嵌入Java脚本以完成复杂功能.但大量的Java脚本使得JSP页面难以维护.一种类似JavaScript语言—EL ...

  6. PHP 浏览器端输出中文正常,cmd中文乱码

    cmd 运行php脚本乱码问题如果别的编码根据下面的自己换吧!chcp 65001 就是换成UTF-8chcp 936 可以换回默认的GBKchcp 437 是美国英语

  7. 网络基础——TCP/IP五层模型

    TCP/IP五层模型 TCP/IP五层协议和OSI的七层协议对应关系如下 在每一层都工作着不同的设备,比如我们常用的交换机就工作在数据链路层的,一般的路由器是工作在网络层的. 在每一层实现的协议也各不 ...

  8. 【Python实践-8】和为S的两个数字

    (剑指offer)输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 思路:选定第一个数字,然后遍历后面的数字求和并与S比 ...

  9. pxc增量备份

    ###增备数据库,如果后续还需要再次增备,则可以再次指定--extra-lsndir,如果与上次备份指定相同的位置,该文件被覆盖# innobackupex --compress --incremen ...

  10. Ubuntu notes

    ubuntu notes Table of Contents 1. backup data 2. Basics Ubuntu 3. Install, uninstall packages 4. Bas ...