Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Help with VB code

How can I easily do the following:  for any string of 2 letters, for example, ag, dm,  ou, etc., I want to generate a string that contains all letters of the alphabet between the 2 letters, inclusive.  And also verify that the first letter precedes the second letter, alphabetically.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
..something like this...
    Sub Main()


        Dim alphabet As String = "abcdefghijklmnopqrstuvwxyz"


        Dim teststring As String = "dm"
        Dim newstring As String = ""
        If teststring.Length = 2 Then
            If alphabet.IndexOf(Left(teststring, 1)) < alphabet.IndexOf(Right(teststring, 1)) Then

                For i = alphabet.IndexOf(Left(teststring, 1)) + 1 To alphabet.IndexOf(Right(teststring, 1)) - 1

                    newstring = newstring & alphabet(i)

                Next

            End If
        End If
      
        MsgBox(newstring)
    End Sub

Open in new window

Version to create an output string
Question: does case matter in other words can you have a string "Ag"
If so do you need to preserve the case?

If not you can do this
Sub Main()
        Dim inp As String = "Ag"
        Dim otp As String

        inp = inp.ToLower()
        If inp.Length = 2 And inp.Chars(0) < inp.Chars(1) Then
            Dim i As Integer
            For i = Asc(inp.Chars(0)) To Asc(inp.Chars(1))
                otp = otp & Chr(i)
            Next
        End If
        Console.WriteLine(otp)
    End Sub

Open in new window

Avatar of HLRosenberger

ASKER

thanks.