Solved

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

Posted on 2004-04-13
19
341 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
ID: 10818792
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
ID: 10818806
That gets the first word in the string I need the others in variables also.  Getting there :)
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818863
maybe a do loop with a split function searching for spaces??
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 27

Expert Comment

by:Dabas
ID: 10818915
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
ID: 10818924
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
ID: 10818943
variable # of spaces
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10818973
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
ID: 10818996
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
ID: 10819034
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819127
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
ID: 10819173
like I said I really need this done without for's..
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 10819218
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
ID: 10819262
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
ID: 10819442
cricket...cricket...cricket
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10819512
bluedragon99,
Have you tried my solution?

Dabas
0
 

Expert Comment

by:Meherab
ID: 10820875
'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
ID: 10821086
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
ID: 10821100
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
ID: 10821316
I agree with you elantra.

Just put a different perspective on it

Cheers ;)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 195
Adding Namespace to the second node of the xml 16 73
Java Loop 6 60
What are programming codes and how do they work? 7 94
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

773 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