Parsing Strings in VB6

String parsing is a major area of programming in VB6 and in most other languages. Basically, you get the user to type something into a textbox and then the program must check the string that the user typed into the textbox. Of course, the string can come from anywhere... a file, over the web, through the Winsock, or somewhere else. String parsing allows this to be examined, fixed, or validated.

Functions available in the Strings module embedded deep inside the VBA section.

These can be viewed in VB by pressing F2 to launch the object browser, set the topleftmost combobox to VBA, and then selecting the Strings module in the leftmost listbox. At first you won't have to use all of these, but you might find some use for them eventually.

Len()

Starting with the easiest function (and a form with a textbox and a commandbutton on it), we can do a check to check if the user has input a string that is at least 10 characters. The check is very simple: just call Len() with the string as the argument. Our check looks like this:
Private Sub Command1_Click()
If Len(Text1.Text) >= 10 Then
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Denied.", vbCritical
End If
End Sub

Left$() and Right$()

The $ at the end of Left$() and Right$() means that the function is going to return a string '$'. If you omit the $, the function will return a variant that will be converted to a string (and the conversion takes time). But, Left$ and Right$ function returns a piece of string from either the left end or the right end of the string that you give it. For example, you can check if a string ends with "ical" by using the Right$ function:
Private Sub Command1_Click()
If Right$(Text1.Text, 4) = "ical" Then  'if textbox text ends with 'ical'
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Denied.", vbCritical
End If
End Sub
The second argument to Right$ is the number of characters that you want to get. In this case, there are 4 characters in "ical" so we put in 4 as the second argument. You can check if a string begins with "Non" similarly with the Left$ function. The Left$ function, like the Right$ function takes two arguments: the first one is the string you are looking at and the second one is the number of characters (length) that you want to look at. The length of "Non" is 3, so the second argument will be 3. The check goes like this:
Private Sub Command1_Click()
If Left$(Text1.Text, 3) = "Non" Then  'if textbox text begins with  'Non'
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Denied.", vbCritical
End If
End Sub
You can also combine Left$ and Right$ functions to check an area inside of a string.
Private Sub Command1_Click()
Dim Four As String
Four = Left$(Text1.Text, 4)  'First four
If Right$(Four, 1) = "-" Then  'check if last character of the first four
'characters is a hyphen.
'Or just check if the fourth character is a hyphen.
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
The above checks the fourth character to see if it is a hyphen. It's really not important to do so many Left$ and Right$ at once. The Mid$ function handles things like this with more efficiency.

Mid$()

The Mid$ returns characters in the middle of the string. Well... actually, it returns characters from anywhere in the string. It's called like this: Mid$(stringgoeshere, startatthisindex, length) So, the above check would be done with Mid$ just like this:
Private Sub Command1_Click()
If Mid$(Text1.Text, 4, 1) = "-" Then  'check if fourth character is '-'
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
The Mid$ function is very important for many string parsing procedures.
Mid$("Iceplug", 4, 1) returns a string containing "p", Mid$("Agdrazek", 5, 2) returns a string containing "az", Mid$("Shopping", 6, 3) returns "ing", so Mid$ can indeed get characters from the end of the string... and also from the beginning, as in Mid$("Finance", 1, 5) returns "Finan".
Mid$(stringhere, starthere)
In addition, the third argument (length) is optional... when you don't pass it to the function, Mid$ will return the rest of the string starting at the second argument's starting position... this is also helpful in situations where you want to skip over some characters at the beginning of the string. This is different from Right$() : with the Right$, you specify how many characters you want from the end of the string, while the two argument Mid$ returns the whole string from a starting position, rather than a length.
Mid$("Internet", 6) returns "net", similar to Right$("Internet", 3) returns "net". In actuality, the two argument Mid$ is more compatible with the other functions (importantly, InStr) which deal with character positions and not lengths from the end of the string.

LCase$() and UCase$()

These two functions convert a string into all lowercase (LCase$) or all uppercase (UCase$). This could be used for aesthetic purposes, for example if you wanted an address or something to appear in all caps, you can take your string and pull it through UCase$(). But, another nice use for it is to do a text-insensitive comparison. "Lakes" does not equal "lakes", "LAKES", or "LaKeS" (ugly bumpy text). With LCase$ or UCase$, you can do this check with ease:
Private Sub Command1_Click()
If LCase$(Text1.Text) = "lakes" Then  'check if Lakes text is in Textbox.
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub

InStr(1, haystackstring, needlestring)

The Instr finds the location of a substring inside of another string. In the line that I wrote above (in the header), the first argument is usually 1. Then, the Instr will find the 'needle' in the 'haystack'. The second string is what you are searching for, and the first string is what you are searching in. The return value for Instr is the index of the first character where the 'needle' is found in the 'haystack'. InStr(1, "philadelphia", "delphi") returns 6, the index of the "d" of delphi in philadelphia. InStr(1, "hampshire", "a") returns 2, the index of the "a". Instr(1, "mauritius", "u") returns 3, the index of the "u". Since u appears more than once in the string, the return index is the index of first occurence of "u". Of course, if the needle is not in the haystack, the index is bad. With Instr, you get a 0 if the 'needle' isn't in the 'haystack'. The nice part with this 0 is that you can use InStr in an If statement by itself to determine if the 'needle' is in the 'haystack'.
Private Sub Command1_Click()
If InStr(Text1.Text, "bat") Then   'if bat is in the textbox text.
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
Now, with InStr, you can change the first argument to a number besides 1. This 1 is where InStr will start looking in the haystack for the needle. If the needle is before this starting point, then Instr will not find it and return 0. What's the use for this? Well, for the starting point, you can put in a location of another character that you found with InStr, so you'll effectively be able to find the first occurence of something that is after something else. For instance, we find the first "p" in "ciripipumi" by doing X = InStr(1, "ciripipumi", "p"). We can then find the next "i" that occurs after the first "p" by doing Y = InStr(X, "ciripipumi", "i") and the value in Y. You can do this as many times as you like... but watch out for invalid indices. If you want to block any errors, you have to check the return value from each Instr call, because you'll get an error if you try to pass a 0 as the first argument. So, after X = InStr(1, "ciripipumi", "p"), you'd have to immediately check If X > 0 Then to continue. Below is an example of doing this check to find the "e" that comes before the "i".
Private Sub Command1_Click()
Dim X As Integer
X = InStr(1, Text1.Text, "i")  'Find the first i.
If X > 0 Then
    If InStr(X, Text1.Text, "e") Then  'if there is an e after an i.
        MsgBox "Approved.", vbExclamation
    Else
        MsgBox "Declined.", vbCritical
    End If
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
Overall, Mid$ and Instr together are suitable to do most string parsing (examples further below).

InStrRev(haystackstring, needlestring, start)

This is just like InStr except it starts from the end and looks toward the beginning. This is helpful when you want to find the last occurence of something in a string. The part that seems to confuse is that the return index is indeed the index from the start of the string. InStrRev("calliope", "l") returns 4, not 3. InStrRev("egregious egg", "eg" returns 11, the index of the "e" (of "eg") that is in "egg". Note, when the needle is not found in the haystack, InStrRev returns 0, just like InStr. Also, notice that the 1 is not at the beginning of the string. The starting point is now the optional third argument. So, if you wanted to traverse a few characters in the string going from right to left, it would look like the example below.
Private Sub Command1_Click()
Dim X As Integer
X = InStrRev(Text1.Text, "i")  'Find the last i.
If X > 0 Then
    If InStrRev(Text1.Text, "e", X) Then  'if there is an e before an i.
    'Notice the X is the 3rd argument
        MsgBox "Approved.", vbExclamation
    Else
        MsgBox "Declined.", vbCritical
    End If
Else
    MsgBox "Declined.", vbCritical
End If
End Sub

Now, for those practical examples:

So, let's say you want the user to type in a city and state, something like "Jackson, Wyoming", and you want to get the city (Jackson) into textbox 2, and then get the state (Wyoming) into textbox 3. First you would confirm that there is a ", " in the string. This would be done with InStr: If InStr(1, Text1.Text, ", "), but since we're going to use the location of this ", " to get the city and state, we go ahead and assign the result of InStr to a variable. X = InStr(1, Text1.Text, ", ") and then If X Then. So, now we have the location of the comma-space in the string and next we have to use our other functions to extract the city and state, such as Mid$. The city can be extracted simply with Mid$ or even with Left$: Text2.Text = Mid$(Text1.Text, 1, X - 1) notice that we subtracted 1 from X. This is so that Mid$ doesn't return the "," that we found with InStr. And of course, Mid$ with the second argument as one is the same as Left$. Text2.Text = Left$(Text1.Text, X - 1). Now, getting the state is a little different... but once again, Mid$ will do it for us. Our starting point is X + 2 (the 2 is because we have to skip 2 characters: the comma and the space). Since Mid$ without the third argument will return the rest of the string, we can just use that. Text3.Text = Mid$(Text1.Text, X + 2).
Private Sub Command1_Click()
Dim X As Integer
X = InStr(1, Text1.Text, ", ")
If X Then
    Text2.Text = Left$(Text1.Text, X - 1)
    Text3.Text = Mid$(Text1.Text, X + 2)
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
Another example: getting the filename from a path, such as C:\blah\stuff\folder\file.txt. Here, you'd use InStrRev to find the last backslash "\" X = InStrRev(Text1.Text, "\") and then Mid$ to get the filename. Text2.Text = Mid$(Text1.Text, X + 1) the + 1 is to skip the backslash when returning the filename.
Private Sub Command1_Click()
Dim X As Integer
X = InStrRev(Text1.Text, "\")
If X Then
    Text2.Text = Mid$(Text1.Text, X + 1)
    MsgBox "Approved.", vbExclamation
Else
    MsgBox "Declined.", vbCritical
End If
End Sub
Stay tuned for more examples.