Strings

[ Using VB ]

    To get any where in programming, you must know how to use strings.  The rest of this page covers:
        Assigning Text To Strings
        Using The String
        Getting The Length Of A String
        Left$ and Right$ Functions
        Mid$ Function
        InStr Function
        UCase and LCase
        LTrim and RTrim


    The following code assigns the text "Hello World!" to the string strText.  You'll notice the Dim statement at the top of the code.  The declares the variable as a string because we are assigning text to it.  

                    Sub Command1_Click ()
                     Dim strText As String           

                    strText = "Hello World!"

                    End Sub

    To make sure this works, you could add a line of code after the string saying "Debug.Print strText".  This will put whatever is in the variable strText (in this case, Hello World!) to the debug window.


    Now you can use the string by either putting it into a label, message box, or any other object that uses text.  The following example puts the string's text onto the form.

                    Sub Command1_Click ()
                   
                    Dim strText As String
                    strText = "Hello World!"
                    Print strText
                   
                    End Sub

                    Strings1.bmp (38326 bytes)


    If the user typed something into a textbox, and you stored that text into a string, it'd be useful to know how long that text is.  The following code will put the number 12 on your form.  That is the number of characters in the string "Hello World!".

                    Sub Command1_Click ()

                    Dim strText As String

                    strText = "Hello World!"
                    Print Len(strText)

                    End Sub

                    Strings2.bmp (39910 bytes)


    The next useful thing to do to a string is get just part of it.  Using the left string (Left$) and right string (Right$) functions and get you just the first few characters of the string or just the last few.

                    Sub Command1_Click ()

                    Dim strText As String
                    strText = "Hello World!"

                    'Print the first 5 characters.
                    Print Left$(strText, 5)

                    End Sub

    When this program is run, it will put the first 5 characters on the form.  In this case, "Hello" would be printed to the form.

                    Strings3.bmp (38326 bytes)


    If you want to get some characters from a string without starting from the beginning or the end, you could use the Mid$ function.  The syntax for this function is:

                     strNewString = Mid$(strString, intStartingFrom, intNumChars)

    The strString would be the string you're using.   intStartingFrom would be the character you want to start from and intNumChars is the number of characters you want to go from the starting position.  To help you better understand, look at the following code...

                    Sub Command1_Click ()

                    Dim strText As String
                    Dim intStartingFrom As Integer
                    Dim intNumChars As Integer

                    strText = "Hello World!"

                    'Starting position
                    intStartingFrom = 3

                    'Length
                    intNumChars = 5

                    'Print it
                    Print Mid$(strText, intStartingFrom, intNumChars)

                    End Sub

                    Strings4.bmp (39114 bytes)

    If you start at the 3 character and go over 5 characters, the new string you get would be "llo W". 


    The InStr function will find where a certain part of a string starts.

                    Sub Command1_Click ()

                    Dim strText As String
                    strText = "Hello World!"
                    Print InStr(strText, "World")   'Locate the word World

                    End Sub

                    Strings5.bmp (39534 bytes)

    This will return "7" because World starts at the seventh character.  If we put in the code...

                    Print InStr(strText, "Visual Basic")

...then the integer returned would be "0" since the string "Visual Basic" is not in the strText string.


    Sometimes, it can be very useful to convert a string to all capital letters or all lower case letters.  If you have a password and you don't want it to be case sensitive, you could convert the real password with the password the user entered both to upper case letters (UCase) and compare them.   Look at the following example...

                    Sub Command1_Click ()

                    Dim strPassword As String, strEnteredPassword As String
                    strPassword = "Let Me In"
                    strEnteredPassword = txtPW

                    If UCase(strEnteredPassword) = UCase(strPassword) Then
                        frmSecret.Show
                    Else
                        MsgBox "Invalid Password"
                    End If

                    End Sub

    In this example, if the user enters "let me in" or "LET ME IN" or "Let me in" etc.... into txtPW, then the code frmSecret.Show will be executed.


    The LTrim and RTrim functions are helpful for getting rid of extra spaces.  If you have a string that says something like "         Cool        " then LTrim(strString) and RTrim(strString) kill the spaces on both ends.  Trim(strString) deletes the spaces on both ends of the string.  Using the RTrim function returns the string "          Cool", LTrim returns "Cool        " and Trim returns "Cool"