Working with Strings

 

Strings are a useful data-type in Visual Basic and they become even more useful when you start to use some of the functions that VB6 provides to work with them.

 

This tutorial will introduce you to the following functions:

Function Description
Len(string) Returns the length of the string
Trim(string) Removes any spaces at the start and end of the string
Mid$(string, start, count) Returns a number of characters from the middle of a string
Left$(string, count) Returns a number of characters on the left of a string
Right$(string, count) Returns a number of characters on the right-hand side of a string
   

Len  

This function counts how many characters there are in the string. This can be really useful if you need to check if the user has entered too many characters in a textbox (if you haven't used the MaxLength property).

Another use is to check if a line of text that you've just read from a textfile is the expected length (you might do this to prevent the user manually editing the textfile).

Example

The output of the Len example programPrivate Sub Form_Load()
    Dim strExample As String
    Dim iStringLength As Integer
    
    ' Assign a sentence to strExample
    strExample = "This is an example string"
    
    ' Find out how many characters are in strExample
    ' and store the value in iStringLength

    iStringLength = Len(strExample)
    
    ' Tell the user how many characters are in the string
    MsgBox "The string has " & Str(iStringLength) & " characters."
End Sub

Trim

Trim is a really useful function that gets rid of any whitespace at the start and end of a string. Whitespace includes spaces and tabs.

This is a really useful way of making sure that the user hasn't accidentally typed a space at the start (or end of a string).

Trim does not remove whitespace from the middle of strings.

Example Code

Output of the example program for TrimPrivate Sub Form_Load()
    Dim strExample As String
    Dim strTrimmed As String
    
    ' Assign a sentence to strExample
    strExample = "   This is an example string    "
    
    ' Trim strExample and store the result in strTrimmed
    strTrimmed = Trim(strExample)
    
    ' Show the string before and after trimming
    MsgBox "Before : **" & strExample & "**" & Chr(10) & Chr(13) & _
           "After  : **" & strTrimmed & "**"
End Sub

Left$

Left$ returns a string that contains a number of characters from the left-hand side of the input string. All you have to do is tell VB6 how many characters you want.

Usage

Left$(string,number_of_characters)

E.g. Left$("Chocolate cakes are yummy",9) would return "Chocolate".

Example code

The output of the example code for Left$Private Sub Form_Load()
    Dim strExample As String
    Dim strLeft As String
    
    ' Assign a sentence to strExample
    strExample = "Chocolate cakes are yummy"
    
    ' Put the 9 left-most characters of strExample into strLeft
    strLeft = Left$(strExample, 9)
    
    ' Display the contents of strLeft
    MsgBox strLeft
End Sub

Right$

Right$ returns a string that contains a number of characters from the right-hand side of the input string. All you have to do is tell VB6 how many characters you want.

Usage

Right$(string,number_of_characters)

E.g. Right$("Chocolate cakes are yummy",5) would return "yummy".

Example Code

Output from the example code for Right$Private Sub Form_Load()
    Dim strExample As String
    Dim strRight As String
    
    ' Assign a sentence to strExample
    strExample = "Chocolate cakes are yummy"
    
    ' Put the 5 right-most characters of strExample into strRight
    strRight = Right$(strExample, 5)
    
    ' Display the contents of strRight
    MsgBox strRight
End Sub

Mid$

Mid$ extracts the characters from the middle of the input string. You must supply the starting point in the string and how many characters to extract.

Usage

Mid$(string,starting_point,number_of_characters)

E.g. Mid$("Chocolate cakes are yummy",11,5) would return "cakes".

Example Code

Private Sub Form_Load()
    Dim strExample As String
    Dim strMid As String
    
    ' Assign a sentence to strExample
    strExample = "Chocolate cakes are yummy"
    
    ' Get 5 characters from strExample, starting at character 11
and store in strMid
    strMid = Mid$(strExample, 11, 5)
    
    ' Display the contents of strMid
    MsgBox strMid
End Sub