C#字符串自增自减算法详解
程序员文章站
2023-12-15 12:17:58
c#实现字符串自增和自减运算,供大家参考,具体内容如下
1.数字从 0-9 变化;
2.字母从 a-z、a-z 变化;
3.其它字符跳过;
4.以上变化依据其asc...
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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。