拆分功能是跳过VB.NET的结果

时间:2017-05-20 13:55:41

标签: vb.net split

所以我正在尝试读取文本文件并使用“!!!”字符数组拆分数据 我在使用它时遇到了很多麻烦,因为它出于某种原因跳过了3s。 所以在数组中,md(),我得到md(0)的正确字符串,但是md(1)和md(2)没有内容。然而,md(3)是split中的第二个参数,md(6)是split中的第3个参数。 Screenshot of the results and code

这是被调用的整个子:

  Public Sub recompile()
    Try
        ' Open the file using a stream reader.
        Using sr As New System.IO.StreamReader("C:\Users\Connor\Documents\MovieTheaterPro\MovieData.txt")
            Dim line As String
            Dim inThisRow As Integer = 0
            Dim rows As Integer = 0
            Dim lineCount = IO.File.ReadAllLines("C:\Users\Connor\Documents\MovieTheaterPro\MovieData.txt").Length
            For i As Integer = 0 To lineCount - 1 Step 1
                line = sr.ReadLine()
                MsgBox(line)
                Dim md As String() = line.Split("!!!")
                Dim addTo As String = ""
                For i2 As Integer = 0 To md.Length - 1 Step 1
                    addTo += "md(" & i2 & "): " & md(i2) & vbNewLine
                Next
                MsgBox(addTo)
                If inThisRow = 6 Then
                    rows += 1
                    inThisRow = 0
                End If
                Dim pb As New PictureBox
                pb.Width = 170
                pb.Height = 200
                pb.Top = 172 + (rows * 213)
                pb.Left = 12 + (227 * inThisRow)
                pb.SizeMode = PictureBoxSizeMode.StretchImage
                If (md.Length > 0) Then
                    pb.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(md(1))))
                End If
                Me.Controls.Add(pb)
                inThisRow += 1
            Next



        End Using
    Catch e As Exception
        Console.WriteLine("The file could not be read:")
        Console.WriteLine(e)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

当分割字符串包含多个字符时,

String.Split具有不同的行为。您可以使用Strings.Split代替(仅限VB)。

Dim md As String() = Strings.Split(line,"!!!")

您可以看到这两个功能之间的区别如下

Sub Main()
        Dim input = "a!!!b!!!c!!!d"
        Dim first() = input.Split("!!!")
        Dim second() = Strings.Split(input, "!!!")

        Console.WriteLine(Join(first, "-"))
        Console.WriteLine(Join(second, "-"))
        Console.Read()
End Sub

输出

  

一个--- B ---电Ç--- d

     

A-B-C-d

总的来说,如果你使用正则表达式进行这种类型的字符串解析操作可能会更好。