1

i want to use split function for splitting string into two parts.The question is how can i save the first part and second part into two different string array for further use?

The text i'm going to process is Name,filepath

Dim clientArr() as string
Dim FileArr() as string
FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
    Templine = LineInput(1)
    Dim DataArr() As String = Templine.Split(",")
    ClientArr = DataArr(0)
    FileArr = DataArr(1)
loop

The error says that string cannot be convented into 1 D array

Thank you

2
  • why do they need to go into 2 different arrays? they are already separated into (0) and (1). Commented May 29, 2014 at 0:45
  • Split converts a string into an array of strings. You are trying to assign individual elements from the split array (i.e individual strings) into the arrays you've created. Are you saying you want to loop round the file and keep all the strings? Commented May 29, 2014 at 0:48

3 Answers 3

1

It is the 21st Century and along with flying cars and robot dogs we have a easy way to keep 2 large sets of related data together in a single container, but able to be referenced individually:

Class myFileParts
     public Property FileName As String
     public Property FilePath As String       

     Public Sub New(fName as string, fPath as String)
         FileName = fName
         FilePath = fPath
     End Sub
End Class

' a container to hold lots of these
Friend myFiles As New List(Of myFileParts)

' filling it up:
Dim TempLine As String
Dim myF As myFileParts

Do Until EOF(1)
    Templine = LineInput(1)
    Dim DataArr() As String = Templine.Split(",")

    ' create a file item 
    f = New myFileParts(DataArr(0),DataArr(1))

    ' add to container
   myFiles.Add(f) 
loop

to use one: (N is the index of the desired file info):

Dim thisFile As string = MyFiles(N).FileName
Dim thisPath As string = MyFiles(N).FilePath

to print them all:

For each f As myFileParts in myFiles
    Console.WriteLine(String.Format("Path = {0}; Name = {1}",
              f.FilePath, f.FileName)
Next f
Sign up to request clarification or add additional context in comments.

1 Comment

better yet, put the arrays inside the myFileParts class ;p that is, if you have any need to keep them lying around the house.
0

The problem is that you declare ClientArr and FileArr as string arrays, which is what you want, but then you try to assign them a string value. DataArr(0) is a string value. You need to put that vcalue into one of the array elements in ClientArr. E.g. CleintArr(5) = DataArr(0).

If you knew the exact number of line in the file before the loop then you could declare the array size for CleintArr and FileArr. Then you would use an index that gets increments in the loop to set the proper array element.

But here is a better approach that will work without knowing the size of the file beforehand. This uses string collections to accumulate the clients and files, and then it converts them to an array once the file has been read.

    Dim clientArr() As String
    Dim FileArr() As String

    Dim ClientList As New List(Of String)
    Dim FileList As New List(Of String)

    FileOpen(1, strpath & "Location.txt", OpenMode.Input)
    Do Until EOF(1)
        Templine = LineInput(1)
        Dim DataArr() As String = Templine.Split(",")
        ClientList.Add(DataArr(0))
        FileList.Add(DataArr(1))
    Loop

    clientArr = ClientLIst.ToArray()
    FileArr = FileList.ToArray()

Comments

0

Do you know how many lines are in the file before you start? If not, you could do something like this using the List class:

Dim clientPart As List(Of String) = New List(Of String)()
Dim filePart As List(Of String) = New List(Of String)()

FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
    Templine = LineInput(1)
    Dim DataArr() As String = Templine.Split(",")
    clientPart.Add(DataArr(0))
    filePart.Add(DataArr(1))
Loop

Dim clientPartStringArray As String() = clientPart.ToArray() 'If you really want an array

If you do, then you need to assign each split to an element of your clientArr and fileArr like:

Dim numLinesInFile As Integer 'If you know this value
Dim curIndex As Integer = 0

Dim clientArray(numLinesInFile) As String
Dim fileArray(numLinesInFile) As String

FileOpen(1, strpath & "Location.txt", OpenMode.Input)
Do Until EOF(1)
    Templine = LineInput(1)
    Dim DataArr() As String = Templine.Split(",")
    clientArray(curIndex) = DataArr(0)
    fileArray(curIndex) = DataArr(1)

    curIndex = curIndex + 1
Loop

Note the differences in your code in terms of:

  • We declare the arrays clientArray and fileArray with a specific size (you'll need to know this in advance - if you don't/can't know this consider using a collection like a List as previously alluded to)
  • On each iteration we assign the strings you get from the Split into a specific index of the array denoted by curIndex
  • We have to increment curIndex by 1 each time so that we write to empty slots in clientArray and fileArray...otherwise you'd just write to the same index (0) each time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.