Search code examples
vb.netstring-function

Word split fails with more then 3 words


I try to split a string every + then every |, it works fine when I try to read only 3 words from the split words1(0-3), but when I try to read words1(4) the whole function fails... here is the code:

Private Function SetUpdateData()
    Try
        Dim delimiterChars As Char() = {"+"c}
        Dim words As String() = updatelist.Split(delimiterChars)

        Dim i As Integer = 1
        Do While (i < words.Length)
            Dim delimiterChars1 As Char() = {"|"c}
            Dim words1 As String() = words(i).Split(delimiterChars1)

            Dim name As String = words1(0)
            Dim version As String = words1(1)
            Dim fileurl As String = words1(2)
            Dim size As String = (words1(3) / 1024D / 1024D).ToString("0.00") & " MB"

            Dim cversion As FileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory() & "\" & name))
            If My.Computer.FileSystem.FileExists(Directory.GetCurrentDirectory() & "\" & name) Then
                If Not version.Contains(cversion.FileVersion) Then
                    DataGridView1.Rows.Add(name, version, size)
                    RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3))
                End If
            Else
                DataGridView1.Rows.Add(name, version, size)
                RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3))
            End If
            i = (i + 1)
        Loop
    Catch ex As Exception
        MsgBox("error")
    End Try
End Function

This above works no problem at all, but when you add words1(4) in like this:

Private Function SetUpdateData()
    Try
        Dim delimiterChars As Char() = {"+"c}
        Dim words As String() = updatelist.Split(delimiterChars)

        Dim i As Integer = 1
        Do While (i < words.Length)
            Dim delimiterChars1 As Char() = {"|"c}
            Dim words1 As String() = words(i).Split(delimiterChars1)

            Dim name As String = words1(0)
            Dim version As String = words1(1)
            Dim fileurl As String = words1(2)
            Dim size As String = (words1(3) / 1024D / 1024D).ToString("0.00") & " MB"
            Dim status As String = words1(4)

            Dim cversion As FileVersionInfo = FileVersionInfo.GetVersionInfo(Path.Combine(Directory.GetCurrentDirectory() & "\" & name))
            If My.Computer.FileSystem.FileExists(Directory.GetCurrentDirectory() & "\" & name) Then
                If Not version.Contains(cversion.FileVersion) Then
                    DataGridView1.Rows.Add(name, version, size)
                    RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
                End If
            Else
                DataGridView1.Rows.Add(name, version, size)
                RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))
            End If
            i = (i + 1)
        Loop
    Catch ex As Exception
        MsgBox("error")
    End Try
End Function

ALSO the string that it is splitting is:

+Thing v2.exe|1.0.0.1|http://example.com/uploads/Thing v2.exe|205824|Primary+Thing v2 DLL.dll|1.0.0.1|http://example.com/uploads/Thing DLL.dll|1097728|Secondary

Which all should output:

words1(0) - Thing v2.exe 
words1(1) - 1.0.0.1 
words1(2) - http://example.com/uploads/Thing v2.exe 
words1(3) - 205824 
words1(4) - Primary

But as I said above once words1(4) is used it crashes the whole function...

It will catch and fail and give the error message, but when I try to do msgbox(ex) for the exception error, no msgbox pops up and the program just continues.

If anyone can fix the issue or give me some help that would be greatly appreciated, thanks in advance and sorry if this is confusing as it is to me too!


Solution

  • There are two loops in your program: Loop1: words1(0)>>Thing v2.exe

    words1(1)>>1.0.0.1

    words1(2)>>http://example.com/uploads/Thing v2.exe

    words1(3)>>205824

    words1(4)>>Primary

    Loop2: words1(0)>>Thing v2 DLL.dll

    words1(1)>>1.0.0.1

    words1(2)>>http://example.com/uploads/Thing DLL.dll

    words1(3)>>1097728

    words1(4)>>Secondary

    it appears that you misspelled your words1(4) to words(4) in below line

     RichTextBox1.AppendText("+" & words1(0) & "|" & words1(1) & "|" & words1(2) & "|" & words1(3) & "|" & words(4))