|
-
Mar 29th, 2023, 10:12 AM
#1
Thread Starter
Addicted Member
VB.NET Increment string
Hi all, I am using this code to increment a string. The string can contain letters, numbers and some special characters and usually works pretty well. Since its difficult to try out many combinations, the one I am having trouble with now is a four digit string like "3067" that should go to "3068" but changes it to "368" unless I have a special character in front of it. I'm not sure if what I am using is the correct code for incrementing various strings. Thanks for any suggestions on this.
Code:
Public Function IncrementString(ByVal Sender As String) As String
Dim Index As Integer
For Item As Integer = Sender.Length - 1 To 0 Step -1
Select Case Sender.Substring(Item, 1)
Case "0" To "9"
Case Else
Index = Item
Exit For
End Select
Next
If Index = Sender.Length - 1 Then
Return Sender & "1" ' Optionally throw an exception ?
Else
Dim x As Integer = Index + 1
Dim value As Integer = Integer.Parse(Sender.Substring(x)) + 1
Return Sender.Substring(0, x) & value.ToString()
End If
End Function
-
Mar 29th, 2023, 10:40 AM
#2
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=
-
Mar 29th, 2023, 10:43 AM
#3
Re: VB.NET Increment string
 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.
-
Mar 29th, 2023, 11:36 AM
#4
Re: VB.NET Increment string
What are the rules for the formation of these strings and for each variant examples of the next number?
-
Mar 29th, 2023, 12:30 PM
#5
Thread Starter
Addicted Member
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
-
Mar 29th, 2023, 12:33 PM
#6
Re: VB.NET Increment string
 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?
-
Mar 29th, 2023, 03:14 PM
#7
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
-
Mar 30th, 2023, 06:35 AM
#8
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');
}
}
-
Mar 30th, 2023, 08:47 AM
#9
Thread Starter
Addicted Member
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.
-
Mar 30th, 2023, 08:48 AM
#10
Thread Starter
Addicted Member
Re: VB.NET Increment string
 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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|