C#学习教程:增加字母数字字符串分享


增加字母数字字符串

我试图使用给定的字符串增加一个字母数字字符串,输入为0000a,然后输出应为0000b

但是,当输入达到定义的限制时,比如zzzzz,它应该重置为00001

我在c#中读过以下字母增量

运用

char c c++ 

似乎是最好的方法。

我有以下课程

 Namespace BatchNo_Generator { class RandomStringGenerator { public enum Mode { ALPHANUMERIC = 1, ALPHA = 2, NUMERIC = 3, } public string genbase(int length, string mode) { int j = 0; if (mode == "ALPHANUMERIC") { StringBuilder s = new StringBuilder(); while (j < length - 1) { s.Insert(0, "0"); j++; } s.Insert(s.Length, "1"); return s.ToString(); } if (mode == "ALPHA") { StringBuilder s = new StringBuilder(); while (j < length - 1) { s.Insert(0, "a"); j++; } s.Insert(s.Length, "a"); return s.ToString(); } if (mode == "NUMERIC") { StringBuilder s = new StringBuilder(); while (j  0; j--) { if (checkend(s[j - 1], endchar, 'a', mode) == true) { s[j] = resetchar(s[j], 'a'); incrementchar(s[j + 1], mode); } else { char c = incrementchar(s[j - 1], mode); s.Remove(j - 1, 1); s.Insert(j - 1, c); break; } } return s.ToString(); } if (mode == "NUMERIC") { for (int j = i; j > 0; j--) { if (checkend(s[j - 1], endchar, '0', mode) == true) { s[j-1] = resetchar(s[j-1], '0'); incrementchar(s[j - 1], mode); } else { char c = incrementchar(s[j - 1], mode); s.Remove(j - 1, 1); s.Insert(j - 1, c); } } return s.ToString(); } if (mode == "ALPHANUMERIC") { for (int j = i; j > 0; j--) { if (checkend(s[j - 1], endchar, '0', mode) == true) { s[j-1] = resetchar(s[j-1], '0'); char c = incrementchar(s[j - 1], mode); } else { { char c = incrementchar(s[j - 1], mode); s.Remove(j - 1, 1); s.Insert(j - 1, c); break; } } } return s.ToString(); } return ""; } public char incrementchar(char c, string mode) { char cnew = c++; switch (mode) { case "ALPHA": { if (char.IsLetter(c) == false) { cnew++; } else { return char.ToLower(c); } break; } case "NUMERIC": { if (char.IsDigit(c) == false) { cnew++; } else { return c; } break; } case "ALPHANUMERIC": { while (char.IsLetterOrDigit(c) == false) { c++; } // if (char.IsLetterOrDigit(c) == false) { cnew++; } // else { return char.ToLower(c); } return char.ToLower(c); break; } } return '?'; } public bool checkend(char current, char end, char start, string mode) { if (current == end) { return true; } else { return false; } } public char resetchar(char inputchar, char defaultchar) { return defaultchar; } public static IEnumerable GetColumns(char startchar, char endchar) { string s = null; for (char c2 = startchar; c2 <= endchar + 1; c2++) { for (char c = 'A'; c <= 'Z'; c++) { if (char.IsLetter(c) == true) { yield return s + char.ToLower(c); } } if (char.IsLetterOrDigit(c2) == true) { s = c2.ToString(); } } } } 

}

我试过了

 RandomStringGenerator test = new RandomStringGenerator(); MessageBox.Show(test.gennext("0zzzz","ALPHANUMERIC",'0')); 

我遇到的问题是此输入0zzzz返回0zzzª

任何帮助,将不胜感激

代码很草率我知道,当我得到一个工作集时,我将清理它

上述就是C#学习教程:增加字母数字字符串分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 public enum Mode { AlphaNumeric = 1, Alpha = 2, Numeric = 3 } public static string Increment(string text, Mode mode) { var textArr = text.ToCharArray(); // Add legal characters var characters = new List(); if (mode == Mode.AlphaNumeric || mode == Mode.Numeric) for (char c = '0'; c <= '9'; c++) characters.Add(c); if (mode == Mode.AlphaNumeric || mode == Mode.Alpha) for (char c = 'a'; c <= 'z'; c++) characters.Add(c); // Loop from end to beginning for (int i = textArr.Length - 1; i >= 0; i--) { if (textArr[i] == characters.Last()) { textArr[i] = characters.First(); } else { textArr[i] = characters[characters.IndexOf(textArr[i]) + 1]; break; } } return new string(textArr); } // Testing var test1 = Increment("0001", Mode.AlphaNumeric); var test2 = Increment("aab2z", Mode.AlphaNumeric); var test3 = Increment("0009", Mode.Numeric); var test4 = Increment("zz", Mode.Alpha); var test5 = Increment("999", Mode.Numeric); var test6 = Increment("zz", Mode.Alpha); 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031429.html

发表评论

邮箱地址不会被公开。 必填项已用*标注