• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 126
  • Last Modified:

Searching through a string

I have a long string that contains values and characters separated by commas. For example,
 "4.00E-3,v,433,-223E5,l,343.4",.. etc
I want to search through this string and get each number out, but I need to keep the numbers in pairs, with the two numbers separated by the character staying together. In the example above, I need to get the 4.00E-3 and the 433 out as a pair (I will be graphing these as an XY pair), so I need go through this entire string. Does this make sense?
Anyone got any ideas on a nice little routine to parse through this string.
Thanks,
All I have is 67 points, :(

Jeff
0
jscharpf
Asked:
jscharpf
  • 2
1 Solution
 
mcriderCommented:
Add this function:


    Function SplitString(lPage As String) As Variant
    Dim iVal As Long
    Dim jVal As Long
    Dim lArray() As String
    jVal = 0
    Do
        iVal = InStr(1, lPage, ",")
        If iVal = 0 Then Exit Do
        iVal = InStr(iVal + 1, lPage, ",")
        If iVal = 0 Then Exit Do
        ReDim Preserve lArray(jVal) As String
        lArray(jVal) = Left$(lPage, iVal - 1)
        lPage = Mid$(lPage, iVal + 1)
        jVal = jVal + 1
    Loop
    If Not lPage = "" Then
        ReDim Preserve lArray(jVal) As String
        lArray(jVal) = lPage
    End If
    SplitString = lArray
    End Function



Then you can do this:

    Dim Element As Variant
   
    For Each Element In SplitString("aaa,bbb,ccc,ddd")
        Debug.Print Element
    Next


Cheers!
0
 
Jeremy_DCommented:
I don't think mcriders solution will give you what you asked for (his example returns "aaa,bbb" and "ccc,ddd" while it should return "aaa,ccc").
The below functions will do what you asked, rsaSplit (Return String Array Split) will return a one-based array containing strings of the form "aaa,ccc",
 raSplit (Return Array Split) returns a one-based two-dimensional array (Array(1 To MaxCount, 1 To 2)) with the pairs of numbers you want. It returns them as string, but you can easily rewrite it to return numbers (change the aResult array to an array of Doubles and change the "= v(i)" and "= v(i + 2)" parts to "= CDbl(v(i))" and "= CDbl(v(i + 2))").
Keep in mind that both functions expect to get a comma-delimmited string with a multiple of three items.

Private Function rsaSplit(str As String) As Variant
    Dim v As Variant
    Dim cnt As Long
    Dim aResult() As String
    Dim i As Integer
   
    v = Split(str, ",")
    cnt = UBound(v)
    ReDim Preserve aResult(1 To ((cnt + 1) \ 3))
    For i = 0 To cnt Step 3
        aResult(i \ 3 + 1) = v(i) & "," & v(i + 2)
    Next i
    rsaSplit = aResult
End Function

Private Function raSplit(str As String) As Variant
    Dim v As Variant
    Dim cnt As Long
    Dim aResult() As String
    Dim i As Integer
   
    v = Split(str, ",")
    cnt = UBound(v)
    ReDim Preserve aResult(1 To ((cnt + 1) \ 3), 1 To 2)
    For i = 0 To cnt Step 3
        aResult(i \ 3 + 1, 1) = v(i)
        aResult(i \ 3 + 1, 2) = v(i + 2)
    Next i
    raSplit = aResult
End Function

Hope this helps.
Jeremy


0
 
Jeremy_DCommented:
I see I've bin a little sloppy with my declarations, you better make the cnt variable an Integer (I don't think a string can contain more than MAXINT characters, and therefore definately no more than MAXINT pairs of numbers).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now