在vb.net中创建顺序的字母数字字符串



我想知道如何创建123ABC1格式的顺序字符串。第一个记录是000AAA0到999AAA0然后是000AAA1一直到999AAA9。接下来是000AAB0,以此类推。这可能吗?谢谢

看一下这个例子:

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

小提琴:https://dotnetfiddle.net/8DyrUG

这将把传入的序列分成三个部分:

  1. 第一部分是前三位数字
  2. 第二部分是中间三个字母
  3. 第三部分是最后一位数字

对于第三部分,它将数字值加1。如果达到10,则将其重置为0,并指示第二部分应该更新。

对于第二部分,尝试从A-Z增加每个单独的字母,如果一个字母被更新为"Z"之前的信随后更新。

对于第一部分,如果中间字母的第一个字母被更新,则整数加1。

如果达到1000AAA9,则抛出异常(指示序列结束)。

最新更新