Re: VB.NET Increment string
You posted this exact same code almost 1 year ago and I and others pointed out many pitfalls and gotcha's with your current approach with regard to many potential formats of serial strings. At that time I also gave you a suggested simple one-line code change that *may* solve the issue for you.
https://www.vbforums.com/showthread....ric&highlight=
Re: VB.NET Increment string
Quote:
Originally Posted by
mikeg71
I'm not sure if what I am using is the correct code for incrementing various strings.
That statement makes programmers shudder, because "for incrementing various strings" is completely meaningless without context. The last time you posted about this, you at least included examples of what types of Strings you are passing in to the Function, and what you want to come back out of the Function.
But the answer is no, it is not the correct code for your needs, because if it was, it would be producing the output you desire.
Re: VB.NET Increment string
What are the rules for the formation of these strings and for each variant examples of the next number?
Re: VB.NET Increment string
'optionbase1' - I completely overlooked my last post from a year ago. That is my fault 100%. See some of my examples below, but like I mentioned, this does work "most" of the time. But one sneaks in that doesn't seem to handle properly and I just want to understand more on how to deal with it.
'dbasnett' - To answer yours, there really isn't any rules that exist for this as there is the possibility of having many variations. These are serial numbers that get assigned for various products and I don't get the chance to see them in advance. But they are typically in the form of and should increment to:
Code:
F-2389 -to- F-2390
F2891 -to- F2892
8057 -to- 8058
8057-D234 -to- 8057-D235
Re: VB.NET Increment string
Quote:
Originally Posted by
mikeg71
'optionbase1' - I completely overlooked my last post from a year ago. That is my fault 100%. See some of my examples below, but like I mentioned, this does work "most" of the time. But one sneaks in that doesn't seem to handle properly and I just want to understand more on how to deal with it.
I explained exactly why this is happening in my first reply to you last year, and as I mentioned above, I offered a very simple change to 1 line of your code that might fix the issue. Did you try it?
Re: VB.NET Increment string
Try this, just some minor tweaks.
Code:
Public Function IncrementString(StringToIncrement As String) As String
Const nums As String = "0123456789"
Dim Index As Integer
For Index = StringToIncrement.Length - 1 To 0 Step -1
If Not nums.Contains(StringToIncrement.Substring(Index, 1)) Then
Exit For
End If
Next
Index += 1
If Index >= StringToIncrement.Length Then
Return StringToIncrement & "1" ' Optionally throw an exception ?
Else
Dim length As Integer = StringToIncrement.Length - Index
Dim value As Integer = Integer.Parse(StringToIncrement.Substring(Index, length)) + 1
Return StringToIncrement.Substring(0, Index) & value.ToString()
End If
End Function
Re: VB.NET Increment string
Try the following
Code:
Imports System.Runtime.CompilerServices
Public Class Form1
Private Sub IncrementButton_Click(sender As Object, e As EventArgs) Handles IncrementButton.Click
Dim values As New List(Of String) From {"F-2389", "F2891", "8057", "8057-D234", "X1 X1"}
For Each value As String In values
Debug.WriteLine($"{value} to {value.Increment}")
Next
End Sub
End Class
Public Module Helpers
<Extension>
Public Function Increment(value As String) As String
If String.IsNullOrWhiteSpace(value) Then
Return ""
End If
Dim characterArray = value.ToCharArray()
For characterIndex = characterArray.Length - 1 To 0 Step -1
Dim characterValue = Convert.ToInt32(characterArray(characterIndex))
If characterValue <> 57 AndAlso characterValue <> 90 AndAlso characterValue <> 122 Then
characterArray(characterIndex) = ChrW(AscW(characterArray(characterIndex)) + 1)
For resetIndex As Integer = characterIndex + 1 To characterArray.Length - 1
characterValue = Convert.ToInt32(characterArray(resetIndex))
If characterValue >= 65 AndAlso characterValue <= 90 Then
characterArray(resetIndex) = "A"c
ElseIf characterValue >= 97 AndAlso characterValue <= 122 Then
characterArray(resetIndex) = "a"c
ElseIf characterValue >= 48 AndAlso characterValue <= 57 Then
characterArray(resetIndex) = "0"c
End If
Next
Return New String(characterArray)
End If
Next
'If we got through the Character Loop and were not able to
'increment anything, we return a NULL string.
Return Nothing
End Function
End Module
C# version
Code:
public static class Helpers
{
public static string Increment(this string sender)
{
string value = Regex.Match(sender, "[0-9]+$").Value;
return sender[..^value.Length] + (long.Parse(value) + 1).ToString().PadLeft(value.Length, '0');
}
}
Re: VB.NET Increment string
@OptionBase1 - I found that I had used your change in another small application and so far it seems to be working. Without investigating it further, I had assumed I made the change to my current project. I will continue trying this out and see how it comes along. Thanks a lot for your time and catching this from a year ago.
Re: VB.NET Increment string
Quote:
Originally Posted by
dbasnett
Try this, just some minor tweaks.
Code:
Public Function IncrementString(StringToIncrement As String) As String
Const nums As String = "0123456789"
Dim Index As Integer
For Index = StringToIncrement.Length - 1 To 0 Step -1
If Not nums.Contains(StringToIncrement.Substring(Index, 1)) Then
Exit For
End If
Next
Index += 1
If Index >= StringToIncrement.Length Then
Return StringToIncrement & "1" ' Optionally throw an exception ?
Else
Dim length As Integer = StringToIncrement.Length - Index
Dim value As Integer = Integer.Parse(StringToIncrement.Substring(Index, length)) + 1
Return StringToIncrement.Substring(0, Index) & value.ToString()
End If
End Function
dbasnett - This also seems to be working. I am going to continue on with these suggestions. Thanks a bunch for the help and your time.