[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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
0
vbkann
Asked:
vbkann
  • 6
  • 3
1 Solution
 
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
 
JuiletteCommented:
What's the call on that sub...
call vb5split(stringvariable,what is szChr, myarrayname)
0
 
manojaminCommented:
the separator (sorry about that!)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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