Solved

Converting a string to an array

Posted on 1998-10-29
3
176 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

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

19 Experts available now in Live!

Get 1:1 Help Now