?
Solved

Converting a string to an array

Posted on 1998-10-29
3
Medium Priority
?
184 Views
Last Modified: 2010-04-30
I have a string read from a text file containing data in the format of: name,status,userfolder,password
For example: Jack,active,user1,beanstalk

Now I need to convert this string to an array so I can use this data. I tried using InStr, Left$ and Right$ searching for the "," on only a string but it got too messy after a while.

The thing is that the string reads "name,status,userfolder,password" and an array will not accept the quote marks. I wonder if there is an easy way to convert it (or just lose the quotes)?
0
Comment
Question by:isidor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
waty earned 150 total points
ID: 1442275
Use the following function :

Public Function GetTokens(sToParse As String, sToken As String) As Variant
   ' *** Returns an array to tokenized values
   ' ***  Ex:  GetTokens("Jack,active,user1,beanstalk", ",") = ({ "Jack", "active", "user1", "beanstalk" })

   Dim nTokenLen       As Integer
   Dim nTokenCnt       As Integer
   Dim nOffset         As Long
   Dim nPrevOffset     As Long
   Dim aTokens()       As String

   nTokenLen = Len(sToken)
   nOffset = InStr(sToParse, sToken)

   Do While nOffset > 0
      ReDim Preserve aTokens(nTokenCnt)
      If nOffset - nPrevOffset > 1 Then
         aTokens(nTokenCnt) = Mid$(sToParse, nPrevOffset + 1, nOffset - 1 - nPrevOffset)
      Else
         aTokens(nTokenCnt) = ""
      End If

      nPrevOffset = nOffset
      nOffset = InStr(nOffset + nTokenLen, sToParse, sToken)
      nTokenCnt = nTokenCnt + 1
   Loop

   ReDim Preserve aTokens(nTokenCnt)
   aTokens(nTokenCnt) = Mid$(sToParse, nPrevOffset + 1)
   GetTokens = CVar(aTokens)

End Function

0
 
LVL 1

Expert Comment

by:slobstar
ID: 1442276
The following example uses the Split function to return an array from a string.
 
Dim MyArray
MyArray = Split("name,status,userfolder,password", ",")

' The "," is used as a delimiter(break-up character)
' MyArray(0) contains "name".
' MyArray(1) contains "status".
' MyArray(2) contains "userfolder".
' MyArray(3) contains "password".

I hope this is what you wanted.  /slobstar
0
 
LVL 18

Expert Comment

by:deighton
ID: 1442277
Private Sub Split(ByVal sString As String, x() As String, n As Long)

'sString your string "name,status,userfolder,password"
'x is your target array
'n is the number of elements returned


    sString = Mid(sString, 2)  'remove leading quotes
    sString = Left(sString, Len(sString) - 1) 'remove final quote
   
    If InStr(sString, ",") = 0 Then
   
        n = 1
        x(n) = sString
       
    Else
       
        While InStr(sString, ",") <> 0
       
            n = n + 1
           
            x(n) = Left(sString, InStr(sString, ",") - 1)
       
            sString = Mid(sString, InStr(sString, ",") + 1)
           
        Wend
   
        If sString <> "" Then
       
            n = n + 1
            x(n) = sString
           
        End If
       
    End If
   
   
End Sub

'sample call
Private Sub Command1_Click()

    Dim z(5) As String
    Dim n As Long

    Call Split("""name,status,userfolder,password""", z, n)
   
    For c = 1 To 5
   
        MsgBox z(c)
       
    Next
    MsgBox n

End Sub

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

719 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