仅递增字符串字母和数字 c#
Increment string letters and numbers only c#
这里有人提出了类似的问题:
Increment a string with both letters and numbers 除了它只增加字符串中的数字,我想增加 both 数字和字母并且只增加字母和数字,而不是像 ;+-*:=
个字符。
示例:
Z006GZYBA1 => Z006GZYBA2
A9 => AA
AZ => B0
1ZZZZZZZZZ => 2000000000
编辑: 我试过的:
public static string IncrementString(this string input)
{
string rtn = "A";
if (!string.IsNullOrWhiteSpace(input))
{
bool prependNew = false;
var sb = new StringBuilder(input.ToUpper());
for (int i = (sb.Length - 1); i >= 0; i--)
{
if (i == sb.Length - 1)
{
var nextChar = Convert.ToUInt16(sb[i]) + 1;
if (nextChar > 90)
{
sb[i] = 'A';
if ((i - 1) >= 0)
{
sb[i - 1] = (char)(Convert.ToUInt16(sb[i - 1]) + 1);
}
else
{
prependNew = true;
}
}
else
{
sb[i] = (char)(nextChar);
break;
}
}
else
{
if (Convert.ToUInt16(sb[i]) > 90)
{
sb[i] = 'A';
if ((i - 1) >= 0)
{
sb[i - 1] = (char)(Convert.ToUInt16(sb[i - 1]) + 1);
}
else
{
prependNew = true;
}
}
else
{
break;
}
}
}
rtn = sb.ToString();
if (prependNew)
{
rtn = "A" + rtn;
}
}
return rtn.ToUpper();
}
这应该可行(方法是 IncrementBase36()
但我将它放在控制台应用程序中用于演示目的):
using System;
namespace Demo
{
class Program
{
static void Main()
{
Console.WriteLine(IncrementBase36("Z006GZYBA1"));
Console.WriteLine(IncrementBase36("A9"));
Console.WriteLine(IncrementBase36("AZ"));
Console.WriteLine(IncrementBase36("1ZZZZZZZZZ"));
}
public static string IncrementBase36(string numberBase36)
{
var digits = numberBase36.ToCharArray();
bool carry = true;
for (int i = digits.Length - 1; i >= 0; --i)
{
if (carry)
{
if (digits[i] == 'Z')
{
digits[i] = '0';
}
else
{
if (digits[i] == '9')
digits[i] = 'A';
else
++digits[i];
carry = false;
}
}
}
if (carry)
return '1' + new string(digits);
else
return new string(digits);
}
}
}
您想要的只是使用 radix = 36
(10
digist + 26
字母)将 1
添加到数字中:
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
这里有人提出了类似的问题:
Increment a string with both letters and numbers 除了它只增加字符串中的数字,我想增加 both 数字和字母并且只增加字母和数字,而不是像 ;+-*:=
个字符。
示例:
Z006GZYBA1 => Z006GZYBA2
A9 => AA
AZ => B0
1ZZZZZZZZZ => 2000000000
编辑: 我试过的:
public static string IncrementString(this string input)
{
string rtn = "A";
if (!string.IsNullOrWhiteSpace(input))
{
bool prependNew = false;
var sb = new StringBuilder(input.ToUpper());
for (int i = (sb.Length - 1); i >= 0; i--)
{
if (i == sb.Length - 1)
{
var nextChar = Convert.ToUInt16(sb[i]) + 1;
if (nextChar > 90)
{
sb[i] = 'A';
if ((i - 1) >= 0)
{
sb[i - 1] = (char)(Convert.ToUInt16(sb[i - 1]) + 1);
}
else
{
prependNew = true;
}
}
else
{
sb[i] = (char)(nextChar);
break;
}
}
else
{
if (Convert.ToUInt16(sb[i]) > 90)
{
sb[i] = 'A';
if ((i - 1) >= 0)
{
sb[i - 1] = (char)(Convert.ToUInt16(sb[i - 1]) + 1);
}
else
{
prependNew = true;
}
}
else
{
break;
}
}
}
rtn = sb.ToString();
if (prependNew)
{
rtn = "A" + rtn;
}
}
return rtn.ToUpper();
}
这应该可行(方法是 IncrementBase36()
但我将它放在控制台应用程序中用于演示目的):
using System;
namespace Demo
{
class Program
{
static void Main()
{
Console.WriteLine(IncrementBase36("Z006GZYBA1"));
Console.WriteLine(IncrementBase36("A9"));
Console.WriteLine(IncrementBase36("AZ"));
Console.WriteLine(IncrementBase36("1ZZZZZZZZZ"));
}
public static string IncrementBase36(string numberBase36)
{
var digits = numberBase36.ToCharArray();
bool carry = true;
for (int i = digits.Length - 1; i >= 0; --i)
{
if (carry)
{
if (digits[i] == 'Z')
{
digits[i] = '0';
}
else
{
if (digits[i] == '9')
digits[i] = 'A';
else
++digits[i];
carry = false;
}
}
}
if (carry)
return '1' + new string(digits);
else
return new string(digits);
}
}
}
您想要的只是使用 radix = 36
(10
digist + 26
字母)将 1
添加到数字中:
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