0
我試圖用一個給定的charset 遞增一個字母數字字符串說輸入的是0000a 則輸出應該0000B遞增的字母數字字符串
然而,當輸入達到定義的限制,說ZZZZZ 它應該重置爲00001
我已閱讀使用以下Increment of Alphabet in 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 < length - 1)
{
s.Insert(0, "0");
j++;
}
s.Insert(s.Length, "1");
return s.ToString();
}
return "";
}
public string gennext(string current, string mode, char endchar)
{
StringBuilder s = new StringBuilder(current);
int i = current.Length;
if (mode == "ALPHA")
{
for (int j = i; 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<string> 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ª
任何幫助不勝感激
的代碼是草率的,我知道,我會清理起來的時候,我得到一個工作組
旁註:馬虎的代碼往往會產生草率的結果...首先考慮清除它...也許簡單複製粘貼從http://stackoverflow.com/questions/8326672/looking-for-a-way-to-have-a-base36-sequence-in-c-sharp可以幫助。 – 2014-10-03 06:13:07
由於您正在以「endChar」的形式發送「0」,因此該方法不明白要執行什麼操作。您應該發送「z」作爲參數「endChar」。從現在開始,您在startChar和endChar上都調用了方法checkend,其值爲「0」。 – Sasse 2014-10-03 06:30:41