Created
August 31, 2025 15:12
-
-
Save karenpayneoregon/0ec8c8ac598604555a1fc63424818563 to your computer and use it in GitHub Desktop.
NextValue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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