Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created August 31, 2025 15:12
Show Gist options
  • Select an option

  • Save karenpayneoregon/0ec8c8ac598604555a1fc63424818563 to your computer and use it in GitHub Desktop.

Select an option

Save karenpayneoregon/0ec8c8ac598604555a1fc63424818563 to your computer and use it in GitHub Desktop.
NextValue
public static partial class Helpers
{
public static string NextValue1(string sender, int incrementBy = 1)
{
var index = sender.Length - 1;
while (index >= 0 && char.IsDigit(sender[index]))
index--;
if (index == sender.Length - 1)
return sender + incrementBy.ToString();
var numberPart = sender[(index + 1)..];
var prefix = sender[..(index + 1)];
var number = long.Parse(numberPart);
var incremented = number + incrementBy;
var resultNumber = incremented.ToString().PadLeft(numberPart.Length, '0');
return prefix + resultNumber;
}
public static string NextValue(string sender, int incrementBy = 1)
{
string value = NumbersPattern().Match(sender).Value;
return sender[..^value.Length] + (long.Parse(value) + incrementBy)
.ToString().PadLeft(value.Length, '0');
}
[GeneratedRegex("[0-9]+$")]
private static partial Regex NumbersPattern();
}
internal class Program
{
static void Main(string[] args)
{
List<string> items = ["Item001", "Item002", "Item009", "Item010", "Item099"];
foreach (var item in items)
{
string next = Helpers.NextValue(item);
Console.WriteLine($"{item} -> {next}");
}
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment