Link to home
Start Free TrialLog in
Avatar of bluedragon99
bluedragon99Flag for United States of America

asked on

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!!
ASKER CERTIFIED SOLUTION
Avatar of elantra
elantra

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bluedragon99

ASKER

That gets the first word in the string I need the others in variables also.  Getting there :)
maybe a do loop with a split function searching for spaces??
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
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
variable # of spaces
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
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
Avatar of elantra
elantra

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%
remember I need to be able to retrieve each value how would I go about calling from the array?
like I said I really need this done without for's..
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?
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)
cricket...cricket...cricket
bluedragon99,
Have you tried my solution?

Dabas
'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

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.
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...
I agree with you elantra.

Just put a different perspective on it

Cheers ;)