-1

字符串匹配的行我想为每一列的该行中的其余值分配变量。这是我试图找到我认为错误的匹配项:

Dim intIndex As Integer
For Each Str As String In strColumn(1)
If Str.Contains(strID) Then
intIndex = Str.IndexOf(strID, 0)
End If
Next

即使找到匹配的行号,我如何告诉每个变量将自己分配给哪一行和哪一列?

一行的样本,每列用“,”分隔:1234567,03/02/2013,7,2,B,L,ST
并且这些行的行数不受限制

4

1 回答 1

0

给你 - 假设你已经有一个字符串(行)数组(可能来自带有 的文件ReadAllLines),这样的事情应该让你朝着正确的方向前进:

Dim rowFound As Boolean = False
Dim rowNumber As Integer = 0
Dim columns As String()
Dim col1 As String = ""
Dim col2 As String = ""
Dim col3 As String = ""
Dim col4 As String = ""
Dim col5 As String = ""
Dim col6 As String = ""
Dim col7 As String = ""

Do While Not rowFound And rowNumber < rowArray.Length
   columns = rowArray(rowNumber).Split(New Char() {","c)
   If columns(0) = strID Then
       rowFound = True
       col1 = colunns(0)
       col2 = columns(1)
       col3 = columns(2)
       col4 = columns(3)
       col5 = columns(4)
       col6 = columns(5)
       col7 = columns(6)
   Else
       rowNumber = rowNumber + 1
   End If
Loop

需要注意的一些事项:

首先,我没有对此进行测试,因此可能存在一些语法错误(我做的 C# 比 VB.NET 多得多)。

其次,我使用了一个 While 循环,这样您就不会浪费时间检查每一行,如果您尽早找到您正在寻找的那一行。如果标志 rowFound 设置为 true 或 rowNumber 计数超过或等于数组的长度(假设从 0 开始的索引数组),While 循环将退出。

在 While 循环中,它拆分每一行并检查 ID。如果匹配,它会使用列值填充变量并将 rowFound 标志设置为 True,从而结束循环。如果没有匹配,它增加 rowNumber 变量并查看下一行(如果有 - 如果没有,While 循环退出)。

如果要将值存储在 String 以外的类型中(您的第二列看起来是 DateTime),则可能需要进行一些转换。

无论如何,这应该让你朝着正确的方向前进。

于 2013-03-20T23:18:38.207 回答