Split()无法正常工作

时间:2016-09-07 21:07:51

标签: vb.net string

我正在进行计算评估,而且我遇到了分裂字符串的问题。由于某种原因,当字符串拆分数组时,整个存储在Variable(0)中。发生的错误是当它尝试为TicketID(Index)分配一个值时,它表示数组超出范围。

以下是代码:

Private Sub ReadInformation(ByRef TicketID() As String, CustomerID() As String, PurchaseMethod() As Char, NumberOfTickets() As Integer, FileName As String)

    Dim Line, TextArray(3) As String
    Dim Index As Integer

    FileOpen(1, FileName, OpenMode.Input)

    For Index = 0 To 499

        Input(1, Line)
        TextArray = Line.Split(",")

        CustomerID(Index) = TextArray(0)
        TicketID(Index) = TextArray(1)
        NumberOfTickets(Index) = TextArray(2)
        PurchaseMethod(Index) = TextArray(3)

        MessageBox.Show(CustomerID(Index))

    Next

    FileClose()

End Sub

这是我试图阅读的TextFile的前10行:

C001,F3,10,S
C002,F3,2,O
C003,F3,3,S
C004,W2,9,S
C005,T3,10,S
C006,F3,2,S
C007,W1,3,O
C008,W3,1,O
C009,T2,2,S
C010,F2,9,O

这是我收到的错误消息:

Error Message

1 个答案:

答案 0 :(得分:0)

我会使用一些列表而不是数组。通过这种方式,您不必担心数组的长度,或者是否有少于500的行。当然,使用File.IO命名空间的更高级的.NET Framework方法是必须的

<div class="fRow">

 <div class="fTitleCell">
    <input id="mainForm:j_idt139" type="checkbox" name="mainForm:j_idt139" class="checkbox-Access">
    <label onmouseover="FA.fixLabelFor(this);">*IP Address*</label>
 </div>

<span class="fInputCell searchField-ip_int" style="width:25.0%;">
    <div xmlns="http://www.w3.org/1999/xhtml" xmlns:ga="http://gfs.com/">
        <input type="text" name="mainForm:j_idt139" value="" class="fieldValidation" onblur="filter.setAdvCheckBox(this);" onkeyup="validation.checkType(this, 'ADDR');">
    </div>
 </span>

</div>

您可以调用此版本的代码来声明4个列表

Private Sub ReadInformation(TicketID As List(Of String), _
                            CustomerID As List(Of String), _
                            PurchaseMethod As List(Of Char), _
                            NumberOfTickets As List(Of Integer), _
                            FileName As String)

    for each line in File.ReadLines(FileName)
        Dim TextArray = Line.Split(","c)
        if TextArray.Length > 3 Then
            CustomerID.Add(TextArray(0))
            TicketID.Add(TextArray(1))
            ' This line works just because you have Option Strict Off
            ' It should be changed as soon as possible
            NumberOfTickets.Add(TextArray(2))
            PurchaseMethod.Add(TextArray(3)) 
        End If
    Next

End Sub

更多面向对象的另一种方法是创建一个代表一行数据的类。在循环内部,您可以创建该类的实例,并将实例添加到单个List

Dim TicketID = New List(Of String)()
Dim CustomerID = New List(Of String)()
Dim PurchaseMethod = New List(Of Char)()
Dim NumberOfTickets = New List(Of Integer)()
ReadInformation(TicketID, CustomerID, PurchaseMethod, NumberOfTickets, FileName)

现在循环变为

Public Class CustomerData
     Public Property TicketID As String
     Public Property CustomerID As String
     Public Property NumberOfTickets As Integer
     Public Property PurchaseMethod As Char
End Class

此版本需要声明一个列表

您可以调用此版本的代码,仅传递文件名并接收函数

的结果
Private Function ReadInformation(FileName As String) as List(Of CustomerData)

    Dim custData = New List(Of CustomerData)()
    For Each line in File.ReadLines(FileName)
        Dim TextArray = Line.Split(","c)
        if TextArray.Length > 3 Then
            Dim data = new CustomerData()
            data.CustomerID = TextArray(0)
            data.TicketID = TextArray(1)
            data.NumberOfTickets = TextArray(2)
            data.PurchaseMethod = TextArray(3)
            custData.Add(data)
        End If
    Next
    return custData
End Function

或将其用作数组

Dim customers = ReadInformation(FileName)
For Each cust in customers
    Console.WriteLine(cust.CustomerID)
    ...
Next