The Left$, Right$ , and Mid$ Functions

 

          This tutorial is for the beginner. It was created with the beginner in mind. I will explain and show a couple examples of how to use each function. All three of the functions are pretty simple to use and very useful for string operations. The Mid$ function is the hardest of the three as it can be alittle tricky(Still easy to use). I will first go over the Left$ function then Right$ then Mid$.

         

          The Left$ function is pretty basic and easy to use.

 

                    Function Left$(String As String, Length As Long) As String

 

          What this will do is return a specified number of characters from the left side of a string. String as String is the string that you want to manipulate. Length as Long is the amount of characters to move from the left of the string.

 

          Example:

 

                    Dim theString As String, leftString As String

       

                    theString = “String Tutorial”

                    leftString = Left$(theString, 6)

                    MsgBox leftString

 

          What I told this function to do was return me the string that is 6 characters from the left of the original string(String Tutorial). What I got was “String”. If you count 6 spaces from the left you will end up with “String”.

 

                    Function Right$(String As String, Length As Long) As String

 

          The Right$ function is used the same way as the Left$ function except it will start from the right of the string instead of the left. Look at the example below.

 

          Example:

 

                     Dim theString As String, rightString As String

       

                    theString = “String Tutorial”

                    rightString = Right$(theString, 8)

                    MsgBox rightString

 

          Pretty much self-explanatory. Told the function to return the string that is 8 charactors from the right of the original string(String Tutorial). What was returned is “Tutorial”. If you count starting from the right 8 spaces you will end up with “Tutorial”.

 

                    Function Mid$(String As String, Start As Long, [Length]) As String

 

          This function can be alittle tricky. This function will return a specified number of characters from a string. String As String is the string that we want to manipulate. Start As Long is where to start counting from in our string. If we set this value to 3 then it will start with “r” in our “String Tutorial” string. If we set this value to 5 then it will start with “n” in our “String Tutorial” string. [Length] is the amount of spaces to move from our Start As Long value.

 

          Example:

 

                    Dim theString As String, midString As String

       

                    theString = “String Tutorial”

                    midString = Mid$(theString, 5, 10)

                    MsgBox midString

 

          Told the function to start within our string 5 characters from the left to the right. We then told the function to count 10 characters from the Start value of our string. The string that was returned to us was “ng Tutoria”. If you start from the beginning of our string and count 5 spaces you will end up at the character “n”. Count 10 spaces from the character “n” you end up at the “a” character. We end up with “ng Tutoria”. Function not that hard at all. Well, I hope you learned something. Hope i didn’t confuse you. :)

 

 

         

 

 

         

 

                   

                   

Leave a Reply