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




Reply With Quote
