Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse tags within string

Posted on 2006-07-20
5
Medium Priority
?
235 Views
Last Modified: 2010-04-30
I have the following string I need to parse and assign the values to textbox's. It looks like the tags are formatted &dataname=. for example I need txtFname.text to = test from the below string.

dim str as string
str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
0
Comment
Question by:Dalexan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Accepted Solution

by:
jmundsack earned 1600 total points
ID: 17148740
(Warning: air code)

Dim arrVars() As String
Dim arrParts() As String
Dim intVar As Integer

arrVars = Split(str, "&")
For intVar = LBound(arrVars) To UBound(arrVars)
    arrParts = Split(arrVars(intVar), "=")
    Select Case arrParts(0)
        Case "customer_id"
            txtCustomerID.Text = arrParts(1)
        Case "authorized"
            txtAuthorized.Text = arrParts(1)
        'etc.
    End Select
Next intVar

0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 400 total points
ID: 17148759
Sub Parse()
    Dim str As String
    Dim i As Integer
    Dim settings() As String
    Dim setting() As String
   
    str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
    settings = Split(str, "&")
    For i = 0 To UBound(settings)
        setting = Split(settings(i), "=")
        Select Case setting(0)
            Case customer_id
                'txtwhatever = setting(1)
            Case "first_name"
                txtFname.Text = setting(1)
            '...
        End Select
    Next i
End Sub

0
 

Author Comment

by:Dalexan
ID: 17148895
Any idea how I can split out the cc_name_on_account tag if there is more than two words

&cc_name_on_account=test+the+order

txtFname.text = test
txtMname.text = the
txtLname.text = order

0
 
LVL 13

Assisted Solution

by:jmundsack
jmundsack earned 1600 total points
ID: 17148940
Just split it one more time, like so:

Dim arrVars() As String
Dim arrParts() As String
Dim intVar As Integer

arrVars = Split(str, "&")
For intVar = LBound(arrVars) To UBound(arrVars)
    arrParts = Split(arrVars(intVar), "=")
    Select Case arrParts(0)
        Case "cc_name_on_account"
            arrParts = Split(arrParts(1), "+")
            txtFname.Text = arrParts(0)
            txtMname.Text = arrParts(1)
            txtLname.Text = arrParts(2)
    End Select
Next intVar

0
 
LVL 9

Expert Comment

by:dancebert
ID: 17149205
Using this requires an array of your textbox names in the same order as they fields (e.g., customer_id) appear in the variable str.  You'd then assign the value to the textbox where the Debug.Print statements are.  This assumes that the order and number of fields in the input string doesn't change.


Private Sub Form_Load()
    Dim str As String
    str = "customer_id=255349&authorized=no&first_name=test&last_name=order&phone=5618662933&email=test%40test.com&product=50&username=&password=&shipping_address=1124+apollo+rd&shipping_unit=1124+apollo+rd&shipping_city=Melbourne&shipping_state=FL&shipping_zip=32935&shipping_country=US&billing_address=1124+apollo+rd&billing_unit=1124+apollo+rd&billing_city=Melbourne&billing_state=FL&billing_zip=32935&billing_country=US&cc_number=5454545454545454&cc_expires=2011-05-30+23%3A59%3A59&cc_name_on_account=test+order&cc_ccid=123"
    Dim parseEquals
    parseEquals = Split(str, "&")
    Dim ii As Long, jj As Long
    Dim KeyValue, parsePluses
    For ii = 0 To UBound(parseEquals)
        KeyValue = Split(parseEquals(ii), "=")
        If InStr(parseEquals(ii), "+") = 0 Then
            Debug.Print "Field: " & KeyValue(0) & ",  Value: " & KeyValue(1)
        Else
            parsePluses = Split(KeyValue(1), "+")
            For jj = 0 To UBound(parsePluses)
                Debug.Print "Field_" & CStr(jj) & ": " & KeyValue(0) & ",  Value: " & parsePluses(jj)
            Next
        End If
    Next

End Sub

'''''''''' output from above

Field: customer_id,  Value: 255349
Field: authorized,  Value: no
Field: first_name,  Value: test
Field: last_name,  Value: order
Field: phone,  Value: 5618662933
Field: email,  Value: test%40test.com
Field: product,  Value: 50
Field: username,  Value:
Field: password,  Value:
Field_0: shipping_address,  Value: 1124
Field_1: shipping_address,  Value: apollo
Field_2: shipping_address,  Value: rd
Field_0: shipping_unit,  Value: 1124
Field_1: shipping_unit,  Value: apollo
Field_2: shipping_unit,  Value: rd
Field: shipping_city,  Value: Melbourne
Field: shipping_state,  Value: FL
Field: shipping_zip,  Value: 32935
Field: shipping_country,  Value: US
Field_0: billing_address,  Value: 1124
Field_1: billing_address,  Value: apollo
Field_2: billing_address,  Value: rd
Field_0: billing_unit,  Value: 1124
Field_1: billing_unit,  Value: apollo
Field_2: billing_unit,  Value: rd
Field: billing_city,  Value: Melbourne
Field: billing_state,  Value: FL
Field: billing_zip,  Value: 32935
Field: billing_country,  Value: US
Field: cc_number,  Value: 5454545454545454
Field_0: cc_expires,  Value: 2011-05-30
Field_1: cc_expires,  Value: 23%3A59%3A59
Field_0: cc_name_on_account,  Value: test
Field_1: cc_name_on_account,  Value: order
Field: cc_ccid,  Value: 123

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question