public static string NextNumber(string number)
{
Regex re = new Regex(@"([a-zA-Z]+)(\d+)");
Match result = re.Match(number);
var newChar = string.Empty;
var newNumber = 1;
var nextChar = 'G';
string alphaPart = result.Groups[1].Value;
string numberPart = result.Groups[2].Value;
if (alphaPart.Length == 1)
{
// if the number has ONE Alphabet.
var letter = alphaPart.ToCharArray().First();
// TO increment number parts;
var numberAlone = Convert.ToInt32(numberPart);
if (numberAlone == 9999)
{
if (letter == 'Z')
{
var newFirstChar = 'A';
var newSecondChar = 'A';
newNumber = 1;
var padResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{newFirstChar}{newSecondChar}{padResult}";
}
else
{
// To incremenet Alpha part.
nextChar = (char)(letter + 1);
numberAlone = 1;
var paddedResult = numberAlone.ToString().PadLeft(4, '0');
newChar = $"{nextChar}{paddedResult}";
}
}
else
{
nextChar = letter;
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(4, '0');
newChar = $"{nextChar}{paddedResult}";
}
}
else
{
// If the number has TWO Alphabet.
// To incremenet Alpha part.
var firstLetter = alphaPart.ToCharArray().First();
var secondLetter = alphaPart.ToCharArray().Last();
var numberAlone = Convert.ToInt32(numberPart);
if (secondLetter != null)
{
if (secondLetter == 'Z')
{
if (numberAlone == 999)
{
var newFirstChar = (char)(firstLetter + 1);
if (firstLetter == 'Z')
{
newFirstChar = 'A';
}
var newSecondChar = 'A';
newNumber = 1;
var paddedResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{newFirstChar}{newSecondChar}{paddedResult}";
}
else
{
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{secondLetter}{paddedResult}";
}
}
else
{
if (numberAlone == 999)
{
var newSecondChar = (char)(secondLetter + 1);
newNumber = 1;
var paddedResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{newSecondChar}{paddedResult}";
}
else
{
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{secondLetter}{paddedResult}";
}
}
}
}
return newChar;
}
{
Regex re = new Regex(@"([a-zA-Z]+)(\d+)");
Match result = re.Match(number);
var newChar = string.Empty;
var newNumber = 1;
var nextChar = 'G';
string alphaPart = result.Groups[1].Value;
string numberPart = result.Groups[2].Value;
if (alphaPart.Length == 1)
{
// if the number has ONE Alphabet.
var letter = alphaPart.ToCharArray().First();
// TO increment number parts;
var numberAlone = Convert.ToInt32(numberPart);
if (numberAlone == 9999)
{
if (letter == 'Z')
{
var newFirstChar = 'A';
var newSecondChar = 'A';
newNumber = 1;
var padResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{newFirstChar}{newSecondChar}{padResult}";
}
else
{
// To incremenet Alpha part.
nextChar = (char)(letter + 1);
numberAlone = 1;
var paddedResult = numberAlone.ToString().PadLeft(4, '0');
newChar = $"{nextChar}{paddedResult}";
}
}
else
{
nextChar = letter;
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(4, '0');
newChar = $"{nextChar}{paddedResult}";
}
}
else
{
// If the number has TWO Alphabet.
// To incremenet Alpha part.
var firstLetter = alphaPart.ToCharArray().First();
var secondLetter = alphaPart.ToCharArray().Last();
var numberAlone = Convert.ToInt32(numberPart);
if (secondLetter != null)
{
if (secondLetter == 'Z')
{
if (numberAlone == 999)
{
var newFirstChar = (char)(firstLetter + 1);
if (firstLetter == 'Z')
{
newFirstChar = 'A';
}
var newSecondChar = 'A';
newNumber = 1;
var paddedResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{newFirstChar}{newSecondChar}{paddedResult}";
}
else
{
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{secondLetter}{paddedResult}";
}
}
else
{
if (numberAlone == 999)
{
var newSecondChar = (char)(secondLetter + 1);
newNumber = 1;
var paddedResult = newNumber.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{newSecondChar}{paddedResult}";
}
else
{
numberAlone += 1;
var paddedResult = numberAlone.ToString().PadLeft(3, '0');
newChar = $"{firstLetter}{secondLetter}{paddedResult}";
}
}
}
}
return newChar;
}