using an array

If i have a string that has multiple different values in it, each
separated by a ",". How can i load these values into an array?

eg i have a originalstring containing "hi,my,town,eat"
i want to put them in an string array..eg

value(1) = "hi"
value(2) = "my"
value(3) = "town"
value(4) = "eat"

There will not always be 4 values...so could the code be valid for
any number of values...

Please help with some code..thanks
LVL 1
vbkannAsked:
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.

manojaminCommented:
If your are using VB 6 use Split function, else here is the code for vb5


Public Sub vb5Split(ByVal szString As String, _
                    ByVal szChar As String, _
                    ByRef szArray() As String)
    Dim lszString As String
    Dim i As Integer
    Dim indx As Integer
    indx = 0
    lszString = szString
    ReDim szArray(indx)
    i = InStr(lszString, szChar)
    While (i > 0)
        ReDim Preserve szArray(indx + 1)
        szArray(indx) = Left(lszString, i - Len(szChar))
        indx = indx + 1
        lszString = Right(lszString, Len(lszString) - i)
        i = InStr(lszString, szChar)
    Wend
    ReDim Preserve szArray(indx)
    szArray(indx) = lszString
'    For i = LBound(szArray) To UBound(szArray)
'        Debug.Print szArray(i)
'    Next i
End Sub


0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
JuiletteCommented:
What's the call on that sub...
call vb5split(stringvariable,what is szChr, myarrayname)
0
manojaminCommented:
the separator (sorry about that!)
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

JuiletteCommented:
Playing with your code!

doesn't like myArray in the call
What am I doing wrong?

Wayne

Private Sub Command1_Click()
Dim x
x = "I,AM,REALLY,CONFUSED,TODAY"
Dim myArray(1 To 5)

Call vb5Split(x, ",", myArray)
Dim i
For i = 1 To 5
    List1.AddItem myArray(i)
    Next
   
End Sub
0
manojaminCommented:
Private Sub Command1_Click()
Dim x
x = "I,AM,REALLY,CONFUSED,TODAY"
'Dim myArray(1 To 5)
Dim myArray()

Call vb5Split(x, ",", myArray)
Dim i
'For i = 1 To 5
For i = LBound(myArray) To UBound(myArray)
    List1.AddItem myArray(i)
Next
     
End Sub
0
manojaminCommented:
sorry this too...
Dim myArray() as String
0
JuiletteCommented:
Tried Tested n' True
Good job manojamin...for others who need a little extra proding....to load a comma seperated string of multiple values
into an array. From Q & A Expert's Exchange  Post: manojamin

'''''''''''''''''''''''''''''''''''''''''''''''''''''
ie...
string x = "HI, I, AM, REALLY, CONFUSED"

Result:
iArray(1) = "HI"
iArray(2) = "I"
iArray(3) = "AM"
iArray(4) = "REALLY"
iArray(5) = "CONFUSED"
''''''''''''''''''''''''''''''''''''''''''''''''''''''

'If your are using VB 6 use Split function,
'else here is the code for vb5
'================================

Option Explicit

Public Sub vb5Split(ByVal szString As String, _
                    ByVal szChar As String, _
                    ByRef szArray() As String)
    Dim lszString As String
    Dim i As Integer
    Dim indx As Integer
    indx = 0
    lszString = szString
    ReDim szArray(indx)
    i = InStr(lszString, szChar)
    While (i > 0)
        ReDim Preserve szArray(indx + 1)
        szArray(indx) = Left(lszString, i - Len(szChar))
        indx = indx + 1
        lszString = Right(lszString, Len(lszString) - i)
        i = InStr(lszString, szChar)
    Wend
    ReDim Preserve szArray(indx)
    szArray(indx) = lszString
   
   ' For i = LBound(szArray) To UBound(szArray)
       'List1.AddItem szArray(i)
     
   ' Next i
End Sub


'<<<<  FORM  EVENT CODE  >>>>>>


Dim myArray() As String
Dim x As String

x = "I,AM,REALLY,CONFUSED,TODAY"

   Call vb5Split(x, ",", myArray)


0
manojaminCommented:
Juilette: glad to help you! where is vb vbkann though?
0
EYESCommented:
your procedure can be accomplished in four lines of code

Public Sub()

Dim MyArray As Variant
Dim Counter As Integer
Dim Str_Delimited As String

Str_Delimited= 'wherever your  
                'delimited string is
                 'coming from

MyArray = Split(Str_Delimited, ",",-1)

For Counter = 0 to Ubound(MyArray)
    ListBox.AddItem MyArray(Counter)
Next
0
manojaminCommented:
EYES,

IT SAYS THAT

'If your are using VB 6 use Split function,
'else here is the code for vb5

!!!!!!!!!!!!!
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.