Solved

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

Posted on 2004-04-13
19
333 Views
Last Modified: 2010-05-18
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!!
0
Comment
Question by:bluedragon99
  • 9
  • 4
  • 4
  • +1
19 Comments
 
LVL 3

Accepted Solution

by:
elantra earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
That gets the first word in the string I need the others in variables also.  Getting there :)
0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
maybe a do loop with a split function searching for spaces??
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
variable # of spaces
0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 3

Expert Comment

by:elantra
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
remember I need to be able to retrieve each value how would I go about calling from the array?
0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
like I said I really need this done without for's..
0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
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
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
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
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
cricket...cricket...cricket
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
bluedragon99,
Have you tried my solution?

Dabas
0
 

Expert Comment

by:Meherab
Comment Utility
'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
 
LVL 3

Expert Comment

by:elantra
Comment Utility
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
 
LVL 3

Expert Comment

by:elantra
Comment Utility
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
 

Expert Comment

by:Meherab
Comment Utility
I agree with you elantra.

Just put a different perspective on it

Cheers ;)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now