C#字符串自增自减算法详解
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
C#实现字符串自增和自减运算,供大家参考,具体内容如下
1.数字从 0-9 变化;
2.字母从 A-Z、a-z 变化;
3.其它字符跳过;
4.以上变化依据其Ascii码值;
/// <summary> /// 字符串运算 /// </summary> public class StringOperation { /// <summary> /// 通过ASCII码值,对字符串自增1 /// </summary> /// <param name="pStr">输入字符串</param> /// <returns></returns> public static string StringIncreaseOne(string pStr) { var vRetStr = pStr; if (0 == pStr.Length) { vRetStr = "1"; } else { // 将最后一个字符与之前的字符串分开 string vOtherStr = pStr.Substring(0, pStr.Length - 1); int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值 if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9) { vIntChar++; //自增1 if (vIntChar == 58) // 进一位 { vIntChar = 48; vOtherStr = StringIncreaseOne(vOtherStr); } } else if (65 <= vIntChar && vIntChar <= 90) //是字母(A - Z) { vIntChar++; //自增1 if (vIntChar == 91) { vIntChar = 65; vOtherStr = StringIncreaseOne(vOtherStr); } } else if (97 <= vIntChar && vIntChar <= 122) //是字母(a - z) { vIntChar++; //自增1 if (vIntChar == 123) { vIntChar = 97; vOtherStr = StringIncreaseOne(vOtherStr); } } else // 其它字符 -> 跳过 { vOtherStr = StringIncreaseOne(vOtherStr); } vRetStr = vOtherStr + (char)vIntChar; } return vRetStr; } /// <summary> /// 通过ASCII码值,对字符串自减1 /// </summary> /// <param name="pStr">输入字符串</param> /// <returns></returns> public static string StringReducingOne(string pStr) { var vRetStr = pStr; if (0 == pStr.Length) { vRetStr = "9"; } else { string vOtherStr = pStr.Substring(0, pStr.Length - 1); int vIntChar = (int)pStr[pStr.Length - 1]; //转ASCII码值 if (48 <= vIntChar && vIntChar <= 57) //是数字(0 - 9) { vIntChar--; if (vIntChar == 47) { vIntChar = 57; vOtherStr = StringReducingOne(vOtherStr); } } else if (65 <= vIntChar && vIntChar <= 90) //是数字(A - Z) { vIntChar--; if (vIntChar == 64) { vIntChar = 90; vOtherStr = StringReducingOne(vOtherStr); } } else if (97 <= vIntChar && vIntChar <= 122) //是数字(a - z) { vIntChar--; if (vIntChar == 96) { vIntChar = 122; vOtherStr = StringReducingOne(vOtherStr); } } else // 其它字符 -> 跳过 { vOtherStr = StringReducingOne(vOtherStr); } vRetStr = vOtherStr + (char)vIntChar; } return vRetStr; } /// <summary> /// 通过ASCII码值,对字符串自增 /// </summary> /// <param name="pStr">输入字符串</param> /// <param name="pCount">自增个数</param> /// <returns></returns> public static string StringIncrease(string pStr, int pCount) { string vRetStr = pStr; for (int i = 0; i < pCount; i++) { vRetStr = StringIncreaseOne(vRetStr); } return vRetStr; } /// <summary> /// 通过ASCII码值,对字符串自减 /// </summary> /// <param name="pStr">输入字符串</param> /// <param name="pCount">自减个数</param> /// <returns></returns> public static string StringReducing(string pStr, int pCount) { string vRetStr = pStr; for (int i = 0; i < pCount; i++) { vRetStr = StringReducingOne(vRetStr); } return vRetStr; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10C#实现实体类与字符串互相转换的方法
- 01-10C#中Json字符串的各种应用类实例讲解
- 01-10VS中C#读取app.config数据库配置字符串的三种方法
- 01-10轻松学习C#的运算符
- 01-10轻松学习C#的String类
- 01-10C#找出字符串中第一个字母并大写的方法
- 01-10C#解析json字符串总是多出双引号的原因分析及解决办法
- 01-10C#利用时间和随即字符串创建唯一的订单编号
- 01-10C#程序中session值的保存方法以及转为字符串的方法总结
- 01-10C#后台接受前台JSON字符串装换成字典集合处理
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?