Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse elements in string with limits

Status
Not open for further replies.

mygmat123

Technical User
Jun 28, 2004
56
0
0
US
I have a string that I need to parse based on the number of elements.

Using the string....
string="a,abc,car,bread,number,the,word,world,never,before"

I would want to parse the string into three items per parsed string, then display.

the end result would be....

msgbox("a,abc,car")
msgbox("bread,number,the")
msgbox("word,world,never")
msgbox("before")


How can I do this? I looked at the split function, but I'm not sure how that loop function would work, or if there is a better method. PLEASE HELP!
 
A starting point:
Dim myString As String, a, i As Integer, j As Integer, u As Integer, s As String
myString = "a,abc,car,bread,number,the,word,world,never,before"
a = Split(myString, ",")
u = UBound(a): i = 0: s =""
For j = 1 To (u + 1) \ 3
Debug.Print a(i) & "," & a(i + 1) & "," & a(i + 2)
i = i + 3
Next
For j = 1 To (u + 1) Mod 3
s = s & "," & a(i)
i = i + 1
Next
If s <> "" Then Debug.Print Mid(s, 2)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Maybe:
[tt]astring = "a,abc,car,bread,number,the,word,world,never,before"
bstring = Left(astring, InStr(Replace(astring, ",", " ", , 2), ",") - 1)
Debug.Print bstring[/tt]
 


Hi,
Code:
Const COMMA = ","
string1 = "a,abc,car,bread,number,the,word,world,never,before"
a = Split(string1, ",")
i = 0
Do Until i > UBound(a)
    s = ""
    For j = i To Application.Min(i + 2, UBound(a))
        s = s & a(j) & COMMA
    Next
    MsgBox Left(s, Len(s) - 1)
    i = i + 3
Loop


Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
one way:
Code:
intStart = 1
Do Until done
    
    For xxx = 1 To 3 Step 1
        intPos = InStr(intStart, strAll, ",", vbTextCompare)
        If intPos > 0 Then
            strDisp = strDisp & Mid(strAll, intStart, (intPos - intStart)) & ","
            intStart = intPos + 1
        Else
            strDisp = strDisp & Mid(strAll, intStart)
            done = True
            Exit For
        End If
    Next
    MsgBox (strDisp)
    strDisp = ""
Loop

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top