How to split variable containing words with spaces into multiple variables in VB6

Hey guys,

I need a way to seperate this line into multiple variables:

VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "

In other words I need each item in that var into seperate vars ie a is = to AT1/0.527 b = 10.13.3.110 etc...

This is being used inside a for next loop and I would like this done in one pass so if possible no for next loops (unless you think it will work!)

Please provide working code for answer to be accepted!  Thanks!!
LVL 1
bluedragon99Asked:
Who is Participating?
 
elantraConnect With a Mentor Commented:
Try this:
-------------------------------
    Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, "     ", vbTab)
    a() = Split(Trim(VarSplitme), vbTab)
    MsgBox a(0)
-------------------------------
0
 
bluedragon99Author Commented:
That gets the first word in the string I need the others in variables also.  Getting there :)
0
 
bluedragon99Author Commented:
maybe a do loop with a split function searching for spaces??
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
DabasCommented:
Hi bluedragon99,
> That gets the first word in the string I need the others in variables also.
Not really
a(0) is the first string (AT1/0.527)
a(1) is the seconds (10.13.3.110)
a(2) the third.

If you want to loop through them, then you can try
For I = 0 to UBound(a)
    MsgBox a(i)
Next

Dabas
0
 
DabasCommented:
bluedragon99,
Just had another read at your question.
What is separating the different sections of your string:
1) A fixed number of spaces?
2) A tab
3) A Variable number of spaces?

Dabas
0
 
bluedragon99Author Commented:
variable # of spaces
0
 
bluedragon99Author Commented:
This puts them on different lines in a msg box, gonna use split and search for vbcrlf I think that will work

 Dim myString
    myString = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    myString = Replace(myString, " ", vbCrLf)
    MsgBox myString
0
 
DabasCommented:
bluedragon99,
Actually does not matter. Elantra's code should still be OK, as he is using Trim
Maybe his code needs to be modified thus:

    Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, " ", vbTab) 'Only one space gets converted into a tab, to set a delimiter
    a = Split(Trim(VarSplitme), vbTab) 'The other spaces should get removed by trim here. NOtice also removal of brackets in a()
    For I = 0 to UBound(a)
        MsgBox a(i)
    Next    

Dabas
0
 
elantraCommented:
This is it right here:

    Dim a() As String
    Dim b() As String
    Dim VarSplitme As String
    Dim j As Integer
    j = 0
    ReDim b(j) As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    a() = Split(Trim(VarSplitme))
    For i% = 0 To UBound(a)
        If a(i%) <> "" Then
            b(j) = a(i%)
            j = j + 1
            ReDim Preserve b(j) As String
            MsgBox b(j - 1)
        End If
    Next i%
0
 
bluedragon99Author Commented:
remember I need to be able to retrieve each value how would I go about calling from the array?
0
 
bluedragon99Author Commented:
like I said I really need this done without for's..
0
 
bluedragon99Author Commented:
The reason I'm not using for/next is because for some reason for instance with elantras solution it does not return to next k% it goes to the next line in the main for loop.  any ideas why?
0
 
bluedragon99Author Commented:
This solution from elantra is what I am looking for.  This works great without for/next junk except for the fact that the spacing is random.  Can anyone make this work with random spacing?
Dim a() As String
    Dim VarSplitme As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    VarSplitme = Replace(VarSplitme, "   ", vbTab)
    a() = Split(Trim(VarSplitme), vbTab)
   
    MsgBox a(1)
    MsgBox a(2)
    MsgBox a(3)
    MsgBox a(4)
    MsgBox a(5)
    MsgBox a(6)
    MsgBox a(7)
    MsgBox a(8)
0
 
bluedragon99Author Commented:
cricket...cricket...cricket
0
 
DabasCommented:
bluedragon99,
Have you tried my solution?

Dabas
0
 
MeherabCommented:
'Try This (Without For/Next Loops)

Dim VarSplitme As String
Dim sTemp As String
Dim arrSplit() As String

Dim iLen As Integer
Dim iCount As Integer

    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
   
    'This removes extra Spaces
    sTemp = VarSplitme
    Do
      iLen = Len(sTemp)
      sTemp = Replace(sTemp, Space(2), Space(1))
    Loop Until iLen = Len(sTemp)
   
    'This Does the split into the array
    arrSplit = Split(Trim(sTemp), Space(1))
   
    'Loop the Array
    iCount = 0
    Do
      MsgBox arrSplit(iCount)
      iCount = iCount + 1
    Loop Until iCount > UBound(a)

    'U can use a For Loop here if you want
    For iCount = 0 to UBound(a)
      MsgBox arrSplit(iCount)
    Next

0
 
elantraCommented:
My post in relation to "This is it right here:" @ 04/13/2004 05:31PM MST will work with random spacing.  Have you tried it?  I already tested it and it works for me.
0
 
elantraCommented:
Here is the code you need.  It is exactly the same as above.  It will work with random spacing.  Each value will be separated into the b() array.  You can call each by using b(0) b(1) b(2) etc...  Enjoy.

   'Start here

    Dim a() As String
    Dim b() As String
    Dim VarSplitme As String
    Dim j As Integer
    j = 0
    ReDim b(j) As String
    VarSplitme = "AT1/0.527     10.13.3.110     Null          10.132.2.10     11 007B 007B     1 "
    a() = Split(Trim(VarSplitme))
    For i% = 0 To UBound(a)
        If a(i%) <> "" Then
            b(j) = a(i%)
            j = j + 1
            ReDim Preserve b(j) As String
        End If
    Next i%

    MsgBox b(0)
    MsgBox b(1)
    MsgBox b(2)
    MsgBox b(3)

    'ETC... ETC...
0
 
MeherabCommented:
I agree with you elantra.

Just put a different perspective on it

Cheers ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.