We help IT Professionals succeed at work.

Parsing using InStr, Mid$, Left$ & Right$

Budster
Budster asked
on
Medium Priority
297 Views
Last Modified: 2006-11-17
I'm working on a project for an intro to vb class I'm confused about.  We have to parse the following string:
address:123 Main Street, City:Mytown, Zip:12345
from a input text field and place the output into their appropriate text fields (i.e. txtAddress.text, txtCity.text, & txtZipcode.text).  I'm able to get the street address and the zipcode out but, am having trouble with the city.  I'm not just looking for the answer here but, an understanding of how it's supposed to be done.  Thanks!
Comment
Watch Question

Commented:
you can do the following:

dim i as integer, j as integer

i=instr(addressstr, "City:") 'position of city
j=instr(i,addressstr,",") 'position of comma after city
txtCity.text = Mid$(addressstr, i+5, j-(i+5))

i hope this help you!
Commented:
From your question, seems like you understand the Left$ and Right$ functions as they are used to get what you say you already have.

The Mid$ function is needed to get the City (as fjaf used in the previous post).

Mid$(String, Start, Length) defines the function.

The Length parameter is optional.

You already have the String, so what you need to do is find the Start position and then get the Length of String the defines the City.

As fjaf shows, the InStr function is used to get the START position of 'City:'.  Since that is the start position, the position that you want to begin collecting chars from is the end of that string.

The InStr function is as follows:

InStr(Start, SearchString, SearchChar, CompareMethod)

The first and last are optional parameters.

What fjaf did was find 'City:' and assigned that position to i.

Then, did a find on the next ',' that started after 'City:' and assigned it to j.

The purpose of that is to isolate the chars between the end of 'City:' and the beginning of the next section of the address (which is indicated by ',' in your example).

The final statement uses that information to get the solution:

txtCity.Text = Mid$(addressstr, StartOfCity: + LengthOfCity:, NextCommaPosition - (StartOfCity: + LengthOfCity))

Hope this helps to explain - basically what you needed with the Mid$ statement was to understand the different parameters of the function, and a way to get the information.

Hopefully you have that now.

If not, post additional questions.

Wileecoy.
TimCotteeHead of Software Services
CERTIFIED EXPERT

Commented:
I would personally attempt to simplify things:

Split the string at each comma:

'Declare the variables
Dim strText As String
Dim strElement As String
Dim intPos As Integer
Dim intElement As Integer
Dim intBegin As Integer
strText = "address:123 Main Street, City:Mytown, Zip:12345"
'Declare an array to hold the elements
Dim aryElements(1 To 3) As String
'Begin the parsing loop
Do
  'Increment the element counter
  intElement = intElement + 1
  'Set the beginning position to the character after the last comma found
  intBegin = intPos + 1
  intPos = InStr(intBegin, strText, ",") 'Find the next comma!
  If intPos = 0 Then
    'If there isn't one then assume the end of the string
    intPos = Len(strText)
  End If
  'Assign the section of the string to the array element
  aryElements(intElement) = Mid(strText, intBegin, intPos - intBegin)
  'trim off everything up to the : character
  aryElements(intElement) = Right(aryElements(intElement), Len(aryElements(intElement)) - InStr(aryElements(intElement), ":"))
Loop Until intPos = Len(strText) ' when the position is at the end we are finished
'Print the results, here you would assign these elements to the respective text boxes or whatever
Debug.Print aryElements(1)
Debug.Print aryElements(2)
Debug.Print aryElements(3)

Commented:
Howabout

strText="address:123 Main Street, City:Mytown, Zip:12345"
vaResults=split(strText,", ")

returns you an array of key:data strings
You can then use

txtAddress.text=split(vaResults(0),":")(1)

to show the stuff after the :

It's pretty horrid code, I'd agree.

Commented:
By the question, Budster needs to use the Left$, Right$ and Mid$ functions to get the information, and is more interested in the Why it works than the How it works.

Commented:
Absolutely; my bad. I should read the question more thoroughly in future.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.