Parse a return string

G

Gary Dolliver

Hi all,
I am receiving a return string that I would like to break into different
segments using VBA, and remove info that is not needed. I have two different
characters that I can use for a start and stop, until the last segment, which
will only have the start character. For example, my return string would look
like this:
&field1=f1value&field2=f2value&field3=f3value&field4=f4value
My end result would be
f1value
f2value
f3value
f4value
The only constant is that there will always be 15 values returned, but some
may be null (field3=&field4=f4value...) - they will always be different
lengths.
Is there a way I can use the & and = to retrieve the values I am looking
for? What about the last value? If it does not have an & to end, will it
work?
Help is always appreciated, thanks
-gary
 
D

Dirk Goldgar

Gary Dolliver said:
Hi all,
I am receiving a return string that I would like to break into different
segments using VBA, and remove info that is not needed. I have two
different
characters that I can use for a start and stop, until the last segment,
which
will only have the start character. For example, my return string would
look
like this:
&field1=f1value&field2=f2value&field3=f3value&field4=f4value
My end result would be
f1value
f2value
f3value
f4value
The only constant is that there will always be 15 values returned, but
some
may be null (field3=&field4=f4value...) - they will always be different
lengths.
Is there a way I can use the & and = to retrieve the values I am looking
for? What about the last value? If it does not have an & to end, will it
work?
Help is always appreciated, thanks


You can easily use the Split function for this:

Dim astrSegments As String()
Dim astrFieldSpec As String()
Dim strFieldName As String
Dim strFieldValue As String
Dim I As Integer

astrSegments = Split(YourReturnString, "&")

If UBound(astrSegments) <> 14 Then
MsgBox "The return string doesn't conform to the spec!"
Else
For I = 0 To 14
astrFieldSpec = Split(astrSegments(I), "=")
strFieldName = astrFieldSpec(0)
strFieldValue = astrFieldSpec(1)

' ... now do something with these ...

Next I
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top