Solved

Converting a string to an array

Posted on 1998-10-29
3
180 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
3 Comments
 
LVL 14

Accepted Solution

by:
waty earned 50 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

790 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