Results 1 to 3 of 3

Thread: how can i loop and parse a string?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    352

    how can i loop and parse a string?

    Say i have this string:

    Code:
    search?Origin=&Width=0&Length=0&widthmeters=0&Lengthmeters=3&Price=0
    How can i parse through each of these origins and width's and so forth and retrieve the name of it and the value? Like say i have that string and i want to retrieve:

    origin = ""

    But i don't know how many of these type of things there are and so forth. Does any get what i'm saying?

  2. #2
    Junior Member
    Join Date
    Sep 2002
    Posts
    23
    Start with
    Dim artStr() As String
    aryStr = Split(strIN,"&")
    then use Instr(1,aryStr(I),"=")
    on each element.

  3. #3
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Here's a way to get all your parameter names and values.

    VB Code:
    1. Type FieldData
    2.     Name As String
    3.     Value As Variant
    4. End Type
    5. Dim Field() As FieldData
    6. 'The above goes in your declarations.
    7.  
    8. Sub MySrtingParser()
    9.  
    10. Dim x As String
    11. Dim varFields As Variant
    12. Dim i As Long
    13. Dim PosOfEqualSign As Long
    14.  
    15. x = "search?Origin=&Width=0&Length=0&widthmeters=0&Lengthmeters=3&Price=0"
    16.  
    17. ' Split field after 8th character ("search?") by the "&" sign.
    18. varFields = Split(Mid$(x, 8), "&")
    19.  
    20. ReDim Field(UBound(varFields))
    21. For i = 0 To UBound(varFields)
    22.     ' Find position of "=" sign in field.
    23.     PosOfEqualSign = InStr(varFields(i), "=")
    24.     ' Set field name and value.
    25.     Field(i).Name = Left$(varFields(i), PosOfEqualSign - 1)
    26.     Field(i).Value = Mid$(varFields(i), PosOfEqualSign + 1)
    27. Next
    28.  
    29. ' Print results.
    30. For i = 0 To UBound(Field)
    31.     Debug.Print Field(i).Name & " = " & Field(i).Value
    32. Next
    33.  
    34. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width