Все, что вам нужно, это добавить 1
к числу, используя radix = 36
(10
digist + 26
буквы):
private static string IncrementString(string value) {
if (null == value)
throw new ArgumentNullException(nameof(value));
HashSet<char> skip = new HashSet<char>(";+-*:=");
int shift = 1;
bool added = false;
StringBuilder sb = new StringBuilder(value.Length + 1);
foreach (char c in value.Reverse()) {
if (skip.Contains(c)) {
sb.Append(c);
continue;
}
int v = (c >= '0' && c <= '9') ? c - '0'
: (c >= 'a' && c <= 'z') ? c - 'a' + 10
: (c >= 'A' && c <= 'Z') ? c - 'A' + 10
: throw new FormatException("Invalid symbol");
added = true;
v += shift;
shift = v / 36;
v %= 36;
sb.Append(v < 10 ? (char)('0' + v) : (char) ('A' + v - 10));
}
if (!added)
throw new FormatException("Only delimiters. Nothing to add.");
return string.Concat(sb.ToString().Reverse());
}
Давайте посмотрим:
string[] tests = new string[] {
"Z006GZYBA1",
"A9",
"AZ",
"1ZZZZZZZZZ",
"123:456:Abc:zzz", // test with delimiters
};
string demo = string.Join(Environment.NewLine, tests
.Select(test => $"{test,20} => {IncrementString(test)}"));
Console.Write(demo);
Результат:
Z006GZYBA1 => Z006GZYBA2
A9 => AA
AZ => B0
1ZZZZZZZZZ => 2000000000
123:456:Abc:zzz => 123:456:ABD:000