I need to use a String Tag that cycles through the letters of the Alphabet.

Thus if I have a two letter tag Starting AA it will loop through to AZ and then go onto BA and so on.

I have come up with the following code which does the job.

Can anyone suggest any better ways I might accomplish this?

VB Code:
  1. Imports System.Text
  2. Module Module1
  3.     Function IsRestOfStr(ByVal Tag As String, ByVal Position As Integer) As Boolean
  4.         ' Checks to see if the rest of the string is all Z's
  5.         IsRestOfStr = False
  6.         Dim StrTag As New StringBuilder("")
  7.         StrTag.Append("Z"c, Tag.Length - (Position + 1))
  8.         IsRestOfStr = (StrTag.ToString = Tag.Substring(Position + 1))
  9.     End Function
  10.     Sub ChangeLetter(ByRef Tag As String, ByVal Position As Integer)
  11.         ' Changes the letter at the specified position
  12.         ' Moves to the next letter in the sequence, if it is Z then resets it to A
  13.         Dim SubStr As String
  14.         SubStr = Tag.Substring(Position, 1)
  15.         If SubStr = "Z" Then
  16.             SubStr = "A"
  17.         Else
  18.             SubStr = Chr(Asc(SubStr) + 1)
  19.         End If
  20.         Tag = Tag.Remove(Position, 1)
  21.         Tag = Tag.Insert(Position, SubStr)
  22.     End Sub
  23.     Sub NextLetter(ByRef Tag As String, ByVal Position As Integer)
  24.         ' Checks the letter at the specified position
  25.         Dim SubStr As String
  26.         If Position = (Tag.Length - 1) Then
  27.             ' If this is the last letter in the string then set to the next letter
  28.             ' If it is Z then set to A
  29.             ChangeLetter(Tag, Position)
  30.         Else
  31.             ' Check to see if the rest of the string is all Z's
  32.             If IsRestOfStr(Tag, Position) Then
  33.                 ' If they are then increment this letter
  34.                 ChangeLetter(Tag, Position)
  35.             End If
  36.         End If
  37.     End Sub
  38.     Sub NextTag(ByRef Tag As String)
  39.         ' Moves the letters in the Tag String to the next in sequence
  40.         ' Runs through the entire string.
  41.         Dim Position As Integer
  42.         For Position = 0 To (Tag.Length - 1)
  43.             NextLetter(Tag, Position)
  44.         Next ' Position = 0 to (Tag.length-1)
  45.     End Sub ' NextTag
  46.     Sub Main()
  47.         ' testing the code
  48.         Dim StartTag As String = "AAA"
  49.         Dim Idx As Integer
  50.         For Idx = 1 To 680
  51.             Console.WriteLine(Idx & " " & StartTag)
  52.             NextTag(StartTag)
  53.         Next ' Idx
  54.         Console.WriteLine("Press Enter to Continue")
  55.         Console.ReadLine()
  56.     End Sub
  57.  
  58. End Module