Solved

Parse tags within string

Posted on 2006-07-20
5
220 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
5 Comments
 
LVL 13

Accepted Solution

by:
jmundsack earned 400 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 100 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 400 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now