BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

     BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
        Micro-Mailing-List

Understanding and Using Visual Basic Part 3
By: Jared Hoylman -

Parsing Strings
Strings are one of the most widely used data types, and yet parsing strings is one of the most mis-understood concepts to Visual Basic programmers. So I will show you how it is done.

• The Len Function
The Len function simply returns the number of characters within a string. For example...

Dim sText As String
Dim lTextLength As Long

sText = "Reynolds Electronics"
lTextLength = Len(sText)

After running this code lTextLength will equal 20, which is the number of characters in the string sText

• The InStr Function
The InStr function will tell you if a string is within a string and where it starts. For example...

Dim sText As String
Dim lElectronics As Long

sText = "Reynolds Electronics"
lElectronics = InStr(sText, "Electronics")

After running this code lElectronics will contain the value 10. If you count over from the beginning of the string you will notice that the word Electronics begins at the tenth letter of sText.

You can also use the Instr function just to determine whether a string is present within another string.

Dim sText As String
Dim lElectronics As Long

sText = "Reynolds Electronics"
If InStr(sText, "Electronics") Then
    MsgBox "Found the word 'Electronics'"
Else
    MsgBox "Did not find the word 'Electronics'"
End If

Run this code and it should tell you that it found the word Electronics. Now try changing sText to something else and run it again to see what happens...

• The Left Function
The Left function returns a specified number of characters from the left side of a string. For example run the following code and the results should show up in your Debug window.

Dim sText As String
Dim sLeft1 As String
Dim sLeft5 As String
Dim sLeft15 As String

sText = "Reynolds Electronics"
sLeft1 = Left$(sText, 1)
sLeft5 = Left$(sText, 5)
sLeft15 = Left$(sText, 15)

Debug.Print "The first letter is: " & sLeft1
Debug.Print "The first 5 letters are: " & sLeft5
Debug.Print "The first 15 letters are: " & sLeft15

• The Right Function
The Right function returns a specified number of characters from the right side of a string. For example run the following code and the results should show up in your Debug window.

Dim sText As String
Dim sRight1 As String
Dim sRight5 As String
Dim sRight15 As String

sText = "Reynolds Electronics"
sRight1 = Right$(sText, 1)
sRight5 = Right$(sText, 5)
sRight15 = Right$(sText, 15)

Debug.Print "The last letter is: " & sRight1
Debug.Print "The last 5 letters are: " & sRight5
Debug.Print "The last 15 letters are: " & sRight15

• The Mid Function
Now the Mid function is a little bit trickier so we are going to take this one a little slower. The Mid function needs three values passed to it, the String to search in, a starting position, and a length. What this function actually does is look in a string, starting at the position you tell it to start at, and retrieve the number of characters that you tell it to. So...

Dim sText As String
Dim sMidText As String

sText = "Reynolds Electronics"
sMidText = Mid$(sText, 3, 8)

After running this code sMidText should equal "ynolds E", which starts at the 3rd letter and goes through the 11th (3+8=11). See how it works..? It should also be noted that if a length is not stated it will return all characters from the starting position to the end of the string, as you will see below...

Putting It All Together
Now for some real fun..! Lets say that you are receiving data from a Basic Stamp. This data is stored in a buffer called sBuffer. Each set of data is separated by an ASCII 13. You want to be able to separate each of these sets of data one by one and display them in the debug window. Here goes...

Dim sBuffer As String
Dim lEnd As Long
Dim sData As String

' This is the data in your buffer
sBuffer = "Data1" & Chr$(13) & "Data2" & Chr$(13) & "Data3" & Chr$(13) & "Data4" & Chr$(13)
lEnd = InStr(sBuffer, Chr$(13)) '  Gets Starting position of ASCII 13

Do  ' Starts the loop
If lEnd > 0 Then    ' If > 0 then we have an ASCII 13 in the buffer
    sData = Left$(sBuffer, lEnd - 1)
   ' sData will be all characters before lEnd
    sBuffer = Mid$(sBuffer, lEnd + 1)
   ' We want to delete the data that we just got from the buffer including
     the ASCII 13

    Debug.Print sData   ' Display the data
    lEnd = InStr(sBuffer, Chr$(13))
   ' Gets Starting position of ASCII 13 in the new buffer

End If
Loop While lEnd > 0 ' Loop while ASCII 13 is still present in the buffer

After running this code you should see Data1 through Data4 show up in your Debug Window.

String manipulation is not really that hard. With the use of theses four functions you can parse any string that you want to. It just takes some planning as to how the data will be presented to you, and how you should go about getting the data that you want out of the big string. ;-)

<< Data Types  | Intro |  Advanced Parsing >>

 

Copyright © 1999-2007
Reynolds Electronics

| Contact Information |