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 ...
随机推荐
- TypeError: slice indices must be integers or None or have an __index__ method
由于除法/自动产生的类型是浮点型,因此出现上述错误,修正方法为,将/更改为// roi_gray_lwpCV = gray_lwpCV[y:y + h // 2, x:x + w] # 检出人脸区域后 ...
- micropython陀螺仪控制舵机
2018-03-1220:14:00 import pyb import time from pyb import Pin xlights = (pyb.LED(2),pyb.LED(3)) MO = ...
- Python3.4的Pillow库实现验证码图片
转自 http://blog.csdn.net/bin381/article/details/41969493 from PIL import Image,ImageDraw, ImageFont, ...
- nodejs——避免判断创建多级目录
基本概念 fs.exists已经弃用,可以使用fs.access判断文件夹是否存在,但是官方的建议是在进行文件操作前不要使用fs.access,官方推荐的方式的是直接进行文件操作,有错误再修改 不建议 ...
- HDU_6017_Girls love 233_(dp)(记忆化搜索)
Girls Love 233 Accepts: 30 Submissions: 218 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- CAD在网页中如何实现嵌套打印?
当用户需要打印两个控件的图纸时,可以采用嵌套打印实现.点击此处在线演示. 实现嵌套打印功能,首先将两个控件放入网页中,js代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 【转载】原 IntelliJ IDEA (idea)引入eclipse web项目
原文地址:http://my.oschina.net/u/1170781/blog/192731 摘要 概述IntelliJ IDEA,以后都简称为idea,鼓捣了很久,看了很多例子才搞出来,希望对其 ...
- [转]SAS盘和SATA盘之间的区别
很多人一提到SAS盘和SATA盘之后,首先想到的是接口方面的区别,SAS的接口速度比SATA高很多,所以认为SAS盘要比SATA盘快,性能高.其实,接口方面的区别并不是主要的,只是很小的一方面.那么, ...
- Python随笔day02
算术运算符 + - * ** / // % 比较运算符 > < == >= <= != Python中提供一种更加简单的比较方式. 当判断 ...
- dp专题备忘录
hdu 1024:基础dp题 hdu 1029:主元素问题,很快的解法,计数器 hdu 1069:LIS hdu 1074:数位dp,数位dp基础 hdu 1257:简单LIS,要仔细分析为什么是求最 ...