# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
Commented:
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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.