Home > Net >  Creating a sequential alphanumeric string in vb.net
Creating a sequential alphanumeric string in vb.net

Time:10-28

I would like to know how to create a sequential string in the format 123ABC1. So first record would be 000AAA0 upto 999AAA0 then 000AAA1 all the way to 999AAA9. Next it would be 000AAB0 and so on and so forth. Is this possible? Thankyou

CodePudding user response:

Take a look at this example:

Private Function NextSequence(Optional existingSequence As String = "") As String
    ' beginning record
    If (String.IsNullOrWhiteSpace(existingSequence)) Then
        Return "000AAA0"
    End If

    ' invalid existing sequence length
    If (existingSequence.Length <> 7) Then
        Throw New ArgumentException("existingSequence is not in a valid format, it is not 7 characters long")
    End If

    ' part 1
    Dim part1String As String = existingSequence.Substring(0, 3)
    Dim part1Integer As Integer
    If (Not Integer.TryParse(part1String, part1Integer)) Then
        Throw New ArgumentException("existingSequence is not in a valid format, it does not start with a 3 digit number")
    End If

    ' part 2
    Dim part2String As String = existingSequence.Substring(3, 3)
    If (Not Char.IsLetter(part2String(0)) OrElse Not Char.IsLetter(part2String(1)) OrElse Not Char.IsLetter(part2String(2))) Then
        Throw New ArgumentException("existingSequence is not in a valid format, the middle part is not 3 sequential letters")
    End If

    ' part 3
    Dim part3String As String = existingSequence.Substring(6)
    Dim part3Integer As Integer
    If (Not Integer.TryParse(part3String, part3Integer)) Then
        Throw New ArgumentException("existingSequence is not in a valid format, it does not end with a 1 digit number")
    End If

    ' get the alpha characters
    Dim alphabet() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
    existingSequence = existingSequence.ToUpper()

    ' increment the sequence
    part3Integer  = 1
    If (part3Integer = 10) Then
        part3Integer = 0
    End If

    Dim shouldIncrease As Boolean = part3Integer = 0
    If (shouldIncrease) Then
        Dim nextCharacterIndex As Integer = Array.IndexOf(alphabet, part2String(2))   1
        shouldIncrease = nextCharacterIndex = alphabet.Length
        If (shouldIncrease) Then
            nextCharacterIndex = 0
        End If
        part2String = new String({part2String(0), part2String(1), alphabet(nextCharacterIndex)})
    End If

    If (shouldIncrease) Then
        Dim nextCharacterIndex As Integer = Array.IndexOf(alphabet, part2String(1))   1
        shouldIncrease = nextCharacterIndex = alphabet.Length
        If (shouldIncrease) Then
            nextCharacterIndex = 0
        End If
        part2String = new String({part2String(0), alphabet(nextCharacterIndex), part2String(2)})
    End If

    If (shouldIncrease) Then
        Dim nextCharacterIndex As Integer = Array.IndexOf(alphabet, part2String(0))   1
        shouldIncrease = nextCharacterIndex = alphabet.Length
        If (shouldIncrease) Then
            nextCharacterIndex = 0
        End If
        part2String = new String({alphabet(nextCharacterIndex), part2String(1), part2String(2)})
    End If

    If (shouldIncrease) Then
        part1Integer  = 1
        If (part1Integer > 999) Then
            Throw New ArgumentOutOfRangeException("existingSequence")
        End If
    End If

    Return part1Integer.ToString("D3") & part2String & part3Integer.ToString("D1")
End Function

Fiddle: https://dotnetfiddle.net/8DyrUG

What this does is break the incoming sequence into three parts:

  1. The first part is the first three digits
  2. The second part is the middle three letters
  3. The third part is the last digit

For the third part it increments the digit value by 1. If it reaches 10, then it resets it back to 0 and indicates that the second part should update.

For the second part tries to increment each individual letter from A-Z and if one letter gets updated to "Z" the letter before is subsequently updated.

For the first part, if the first letter of the middle letters was updated then it increments the integer.

If it reaches 1000AAA9, then an exception is thrown (indicating the end of the sequence).

  • Related